2014-10-01 76 views
0

我一直在努力测试我的公司类与测试床主要一段时间的代码。我得到了我想要的所有结果,但是在完成主要功能后,程序崩溃,给出 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

我可以发布更多,如果你们需要它谢谢你。

+1

您可能要完成的三连胜并实现**拷贝构造**。你没有达到[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

+0

'name'和'phone'的'std :: string',并观察所有(当前报告的)麻烦消失。作为奖励,您将不再需要构造函数,析构函数或赋值运算符。 – Chad 2014-10-01 19:28:17

+0

您可以从调试器的“调用堆栈”窗口轻松得知它是公司的析构函数失败。因为你已经明确地称呼它。不要这样做。 – 2014-10-01 19:29:20

回答

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; } 
+0

这是一个班。这样做并不是因为它效率最高,而仅仅是为了处理材料。这也是我们应该如何构建它的。 – kisamefishfry 2014-10-02 01:01:45

+1

即使你不应该使用'std :: string',并且自己做'char *'管理,可靠的做法是把它放在_own_'String'类中。你的公司类可以使用你的String类。准确地解释为什么这是正确的在600字符评论中有点困难,但要点是每个类应该有一个明确的任务,并且“公司”类不应该也管理字符串缓冲区。真正的问题发生在一个做多件事情的班级未能完成其中一件事时,例如内存不足。对于只有1件事情需要清理的dtors来说,清理是唯一可靠的。 – MSalters 2014-10-02 07:59:15