我一直在研究这段代码,试图用test bed main测试我的公司类。我得到了我想要的所有结果,但是在完成主要功能后,程序崩溃了 this message: (我道歉但我是新来的,我还不能直接发布图片。) 我一直在处理这个错误大约需要4-5个小时,而且我发现的一切似乎都不起作用。从我环顾四周的情况来看,我觉得更好地理解指针,但我似乎无法找到问题所在。
//COMPANY.H
#ifndef _COMPANY_H
#define _COMPANY_H
#include <stddef.h>
#include <fstream>
#include <iomanip>
enum { PHONE_LEN = 10 };
enum { MAX_NAME_LEN = 30 };
#define _TESTING_COMP
#ifdef _TESTING_COMP
#include <iostream>
using namespace std;
#endif
class Company
{
public:
Company();
~Company();
Company &operator =(Company const & c);
bool operator==(const Company & c)const {return (*name == *c.name);}
bool operator!=(const Company & c)const {return (*name != *c.name);}
bool operator<(const Company & c)const {return (*name < *c.name);}
friend istream & operator>>(istream & in, Company & c);
friend ostream & operator<<(ostream & out, const Company & c);
private:
char *name; // allocate memory (use new) - zero-terminated
char phone[PHONE_LEN]; // NOT zero-terminated (be careful!)
};
#endif
这是我的头文件,包含函数和数据声明。
//COMPANY.CPP
#include "Company.h"
Company::Company()
{
name = new char [MAX_NAME_LEN];
}
Company::~Company()
{
delete [] name;
}
Company &Company::operator =(Company const & c)
{
for (int i = 0; i < MAX_NAME_LEN; i++)
*(name + i) = *(c.name + i);
for (int i = 0; i < PHONE_LEN; i++)
phone[i] = c.phone[i];
return *this;
}
istream & operator>>(istream & in, Company & c)
{
in >> c.name;
for (int i = 0; i < PHONE_LEN; i++)
in >> c.phone[i];
return in;
}
ostream & operator<<(ostream & out, const Company & c)
{
//because value is a pointer the * is needed to input a value
out << c.name << setiosflags(ios::left) << setw(MAX_NAME_LEN) << "";
for (int i = 0; i < PHONE_LEN; i++)
out << c.phone[i];
out << endl;
return out;
}
#ifdef _TESTING_COMP
void main ()
{
char end;
Company test1, test2;
cin >> test1;
cout << test1;
cin >> test2;
cout << test2;
cout << "testing comp1 < comp2: expecting (0)" << (test1 < test2) << endl;
cout << "testing comp2 < comp1: expecting (1)" << (test2 < test1) << endl;
cout << "testing comp2 == comp1: expecting (0)" << (test2 == test1) << endl;
cout << "testing comp2 != comp1: expecting (1)" << (test2 != test1) << endl;
cout << "set test1 to = test2" << endl;
test1 = test2;
cout << test1;
cout << test2;
cout << "testing comp1 < comp2: expecting (0)" << (test1 < test2) << endl;
cout << "testing comp2 < comp1: expecting (0)" << (test2 < test1) << endl;
cout << "testing comp2 == comp1: expecting (1)" << (test2 == test1) << endl;
cout << "testing comp2 != comp1: expecting (0)" << (test2 != test1) << endl;
test1.~Company();
test2.~Company();
cin >> end;
}
#endif
这是保存额外代码和testbed main的cpp文件。
如果你们可以帮助我解决这个用户错误,那将是非常有必要的。即使你不能感谢时间。我不完全确定我应该寻找什么 these are the data values at the end of the program
如果你们需要,我可以发帖更多,谢谢你。
答案 0 :(得分:1)
这么多的样板是不必要的。通过动态分配name
,你可以毫无理由地做一大堆家务管理(最终导致你的崩溃)。此外,您的所有比较功能都会给您带来惊人的效果。
这是你的类重构为cannonical C ++:
#include <string>
class Company
{
public:
const std::string& get_name() const { return name; }
const std::string& get_phone() const { return phone; }
private:
std::string name;
std::string phone;
friend istream & operator>>(istream & in, Company & c);
friend ostream & operator<<(ostream & out, const Company & c);
};
bool operator==(const Company& lhs, const Company& rhs) { return lhs.name == rhs.name; }
bool operator!=(const Company& lhs, const Company& rhs) { return !(lhs == rhs); }
bool operator<(const Company& lhs, const Company& rhs) { return lhs.name < rhs.name; }