我一直在努力测试我的公司类与测试床主要一段时间的代码。我得到了我想要的所有结果,但是在完成主要功能后,程序崩溃,给出 this message: (我很抱歉,但我是新来的,我无法直接发布图像) 我一直在处理这个问题大概四到五个小时的错误跨越几天,我发现的一切似乎都不起作用。我觉得从整体上看,整个指针更好,但我仍然无法找到问题所在。表达式:_BLOCK_TYPE_IS_VALID删除指针的问题
//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
这是包含额外代码和测试床主的cpp文件。
如果你们可以帮我解决这个用户错误,它将是非常有义务的。即使你不能感谢时间。我不完全确定我应该找什么 these are the data values at the end of the program
我可以发布更多,如果你们需要它谢谢你。
您可能要完成的三连胜并实现**拷贝构造**。你没有达到[The Rule of Three]的要求(http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))。而这个:'test1。〜Company()'应该不会**完成。 – WhozCraig 2014-10-01 19:25:13
'name'和'phone'的'std :: string',并观察所有(当前报告的)麻烦消失。作为奖励,您将不再需要构造函数,析构函数或赋值运算符。 – Chad 2014-10-01 19:28:17
您可以从调试器的“调用堆栈”窗口轻松得知它是公司的析构函数失败。因为你已经明确地称呼它。不要这样做。 – 2014-10-01 19:29:20