我第一次尝试使用移动构造函数和移动赋值运算符,但是当我使用 clang++ 编译时:
c++ -o image -O3 image.cpp -std=c++0x -stdlib=libc++
我收到以下错误消息:
image.cpp:125:11: error: call to implicitly-deleted copy constructor of 'Image'
Image res = sample;
我真的不明白这意味着什么以及如何解决这个问题?我复制了整个代码。任何帮助将不胜感激。
PS:我在网上查看并找不到有关此错误消息的任何信息(除了 2011/2012 年与 clang++ 中的错误相关的一些帖子,我相信到那时该错误会得到修复)。
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <cassert>
class Image
{
public:
Image() : w(512), h(512), d(NULL)
{
//printf("constructor default\n");
d = new float[w * h * 3];
memset(d, 0x0, sizeof(float) * w * h * 3);
}
Image(const unsigned int &_w, const unsigned int &_h) : w(_w), h(_h), d(NULL)
{
d = new float[w * h * 3];
memset(d, 0x0, sizeof(float) * w * h * 3);
}
// move constructor
Image(Image &&img) : w(0), h(0), d(NULL)
{
w = img.w;
h = img.h;
d = img.d;
img.d = NULL;
img.w = img.h = 0;
}
// move assignment operator
Image& operator = (Image &&img)
{
if (this != &img) {
if (d != NULL) delete [] d;
w = img.w, h = img.h;
d = img.d;
img.d = NULL;
img.w = img.h = 0;
}
return *this;
}
//~Image() { if (d != NULL) delete [] d; }
unsigned int w, h;
float *d;
};
int main(int argc, char **argv)
{
Image sample;// = readPPM("./lean.ppm");
Image res = sample;
return 0;
}