Class 析构函数导致重载 + 运算符出现问题
Class destructor is causing problem with overloading + operator
我无法弄清楚我的 C++ class(使用 Visual Studio)有什么问题。 Visual Studio 没有给出任何想要的输出,只是说“退出代码 -1073741819”。我使用原始指针创建了一个名为 Complex 的 class,当调用无参数或参数化构造函数时,使用 new int 分配内存,当变量超出范围时,析构函数使用 delete 关键字释放内存。我面临的唯一问题是,我的 + 运算符重载导致了问题,我很确定这个问题与析构函数有关,当我删除析构函数代码时,+ 运算符工作得很好。或者当我不使用 + 运算符时,程序也可以正常工作。请帮助我找出代码。请不要对我说“你在这里不需要原始指针”,实际上我被告知这样做(仅使用指针)。我坚持了好几个小时。
这是我的代码,请看一遍,包括+运算符重载代码和析构函数代码。
#include<iostream>
using namespace std;
class Complex {
private:
int *real;
int *complex;
public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}
};
// no-args constructor
Complex::Complex() {
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex &rhs) {
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a,b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5,-9);
Complex n2(5,-1);
Complex n3;
n3=n1 + n2;
n3.disp();
return 0;
}
您没有在复制构造函数中分配任何内存,因此您的分配恰好是未初始化的内存。
Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
如果我改成这样:
Complex::Complex(const Complex& source) : Complex() {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
你的程序输出(10,-10)
编辑:评论中的问题。
我已经在您的程序中添加了一些打印输出以准确显示正在发生的事情:
#include<iostream>
using namespace std;
class Complex {
private:
int* real;
int* complex;
public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
std::cout << "destructor" << std::endl;
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}
};
// no-args constructor
Complex::Complex() {
std::cout << "constructor" << std::endl;
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
std::cout << "(x,y)constructor" << std::endl;
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) : Complex() {
std::cout << "copy constructor" << std::endl;
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex& rhs) {
std::cout << "op+" << std::endl;
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a, b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
std::cout << "op=" << std::endl;
*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5, -9);
Complex n2(5, -1);
Complex n3;
n3 = n1 + n2;
n3.disp();
return 0;
}
现在 运行 您的程序结果如下:
constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
constructor
copy constructor
destructor
op=
destructor
(10,-10)
destructor
destructor
destructor
如您所见,其中有一个“复制构造函数”。特别是这一行:n3 = n1 + n2;
导致此打印输出:
op+ // n1 + n2
constructor // base constructor from param constructor
(x,y)constructor // constructing the return value: Complex temp(a, b);
constructor // base constructor from copy constructor
copy constructor // copying from temp to the return value
destructor // destroying temp
op= // assigning the return value to n3
destructor // destroying the return value
顺便说一下,这是在调试模式下编译的。如果我在发布模式下编译,输出会改变:
constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
op=
destructor
(10,-10)
destructor
destructor
destructor
这里的相关点是编译器设法优化了复制构造函数,认识到构造 temp
没有意义,然后复制并销毁它。但这只有在打开优化时才会发生。
我怀疑你的参数化构造函数是问题所在...在你的参数化构造函数中,你正在创建一个 Complex class 对象,它采用 *real 和 *complex 指针,将其指向传入整数(x 和 y)。没有分配内存因此当你的程序结束时,你的析构函数被调用并尝试释放 n1 和 n2 中从未动态分配的内存。
我有几个月没有接触过 C++,所以我可能是错的。请随时检查并告诉我您的结果。
我无法弄清楚我的 C++ class(使用 Visual Studio)有什么问题。 Visual Studio 没有给出任何想要的输出,只是说“退出代码 -1073741819”。我使用原始指针创建了一个名为 Complex 的 class,当调用无参数或参数化构造函数时,使用 new int 分配内存,当变量超出范围时,析构函数使用 delete 关键字释放内存。我面临的唯一问题是,我的 + 运算符重载导致了问题,我很确定这个问题与析构函数有关,当我删除析构函数代码时,+ 运算符工作得很好。或者当我不使用 + 运算符时,程序也可以正常工作。请帮助我找出代码。请不要对我说“你在这里不需要原始指针”,实际上我被告知这样做(仅使用指针)。我坚持了好几个小时。
这是我的代码,请看一遍,包括+运算符重载代码和析构函数代码。
#include<iostream>
using namespace std;
class Complex {
private:
int *real;
int *complex;
public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}
};
// no-args constructor
Complex::Complex() {
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex &rhs) {
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a,b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5,-9);
Complex n2(5,-1);
Complex n3;
n3=n1 + n2;
n3.disp();
return 0;
}
您没有在复制构造函数中分配任何内存,因此您的分配恰好是未初始化的内存。
Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
如果我改成这样:
Complex::Complex(const Complex& source) : Complex() {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
你的程序输出(10,-10)
编辑:评论中的问题。
我已经在您的程序中添加了一些打印输出以准确显示正在发生的事情:
#include<iostream>
using namespace std;
class Complex {
private:
int* real;
int* complex;
public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
std::cout << "destructor" << std::endl;
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}
};
// no-args constructor
Complex::Complex() {
std::cout << "constructor" << std::endl;
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
std::cout << "(x,y)constructor" << std::endl;
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) : Complex() {
std::cout << "copy constructor" << std::endl;
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex& rhs) {
std::cout << "op+" << std::endl;
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a, b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
std::cout << "op=" << std::endl;
*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5, -9);
Complex n2(5, -1);
Complex n3;
n3 = n1 + n2;
n3.disp();
return 0;
}
现在 运行 您的程序结果如下:
constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
constructor
copy constructor
destructor
op=
destructor
(10,-10)
destructor
destructor
destructor
如您所见,其中有一个“复制构造函数”。特别是这一行:n3 = n1 + n2;
导致此打印输出:
op+ // n1 + n2
constructor // base constructor from param constructor
(x,y)constructor // constructing the return value: Complex temp(a, b);
constructor // base constructor from copy constructor
copy constructor // copying from temp to the return value
destructor // destroying temp
op= // assigning the return value to n3
destructor // destroying the return value
顺便说一下,这是在调试模式下编译的。如果我在发布模式下编译,输出会改变:
constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
op=
destructor
(10,-10)
destructor
destructor
destructor
这里的相关点是编译器设法优化了复制构造函数,认识到构造 temp
没有意义,然后复制并销毁它。但这只有在打开优化时才会发生。
我怀疑你的参数化构造函数是问题所在...在你的参数化构造函数中,你正在创建一个 Complex class 对象,它采用 *real 和 *complex 指针,将其指向传入整数(x 和 y)。没有分配内存因此当你的程序结束时,你的析构函数被调用并尝试释放 n1 和 n2 中从未动态分配的内存。
我有几个月没有接触过 C++,所以我可能是错的。请随时检查并告诉我您的结果。