0x00007FFF43D9499E (ucrtbased.dll)处(位于 类和对象.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000000 时发生访问冲突。
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(int a)
{
age = new int(a);
}
Person& operator=(Person& p)
{
if (age != NULL)
{
delete age;
age = NULL;
}
age = new int(*p.age);
return *this;
}
int *age;
~Person()
{
if (age != NULL)
{
delete age;
age = NULL;
}
}
};
int main(void)
{
Person a(19);
Person b(20);
Person c(39);
c = b = a;
cout << a.age << endl;
cout << b.age << endl;
cout << c.age << endl;
system("pause");
return 0;
}
26行,int *age; 这行代码要放在第5行
我运行没有问题啊,我觉得代码也没有问题
我这里也可以运行。我开始怀疑你的vs有问题:)
我这里也可以运行。我开始怀疑你的vs有问题:)
我的环境Win10、vs2019运行你的代码没有问题
不过按照c++标准,几个构造函数规则,要么全是默认,要么都得自己写
https://blog.csdn.net/u014265928/article/details/116896965
可以运行,环境没配好?
这个一般就是空指针导致的或者数组越界等也会产生
这个一般就是空指针导致的或者数组越界等也会产生
代码可以运行,不过为了完备,你最好还应该提供拷贝构造函数,拷贝构造函数和赋值函数的参数都应该是const Person&
类型