gpt4 book ai didi

c++ - 表达式 : _BLOCK_TYPE_IS_VALID problems with deleting pointers

转载 作者:行者123 更新时间:2023-11-28 06:40:00 25 4
gpt4 key购买 nike

我已经在这段代码上工作了一段时间,试图用测试床 main 测试我的 Company 类。我得到了我想要的所有结果,但是在完成主要功能后程序崩溃了 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

这是包含额外代码和主要测试平台的 cpp 文件。

如果你们能帮我解决这个用户错误,我将不胜感激。即使你不能感谢时间。我不完全确定我应该寻找什么 these are the data values at the end of the program

如果你们需要的话我可以发更多谢谢。

最佳答案

这么多样板是不必要的。通过动态分配 name,您会无缘无故地被迫做一大堆内务处理(最终导致崩溃)。此外,您所有的比较功能都会给您带来惊人的结果。

这是重构为规范 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; }

关于c++ - 表达式 : _BLOCK_TYPE_IS_VALID problems with deleting pointers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26148959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com