在 C++ 中乱搞类,遇到一个错误,指出我试图引用一个已删除的函数。这是错误
C2280(Test &Test::operator = (const Test& : 试图引用已删除的函数)。
这是我的代码:
#include "pch.h"
#include <iostream>
using namespace std;
class Test {
public:
int size;
double* array;
public:
Test();
Test& operator=(Test&& a);
Test(int sizeArg) {
size = sizeArg;
array = new double[size];
}
Test(Test&& arg) {
size = arg.size;
array = arg.array;
arg.size = 0;
arg.array = nullptr;
}
~Test()
{
if (array != nullptr) {
delete[]array;
}
}
};
int main()
{
Test outTest;
int x = 1;
//Wont work since looking for a deleted function
if (x = 1) {
Test arg(200000000);
outTest = arg;
}
cout << outTest.array;
}
问题出main()
在等号上。