I am experimenting for the first time with the move constructor and the move assignment operator but when I compile using clang++:
c++ -o image -O3 image.cpp -std=c++0x -stdlib=libc++
I get the following error message:
image.cpp:125:11: error: call to implicitly-deleted copy constructor of 'Image'
Image res = sample;
I really don't understand what that means and how to fix this? I copied the entire code. Any help would be greatly appreciated.
PS: I looked on the web and couldn't find anything about this error message (apart some post from 2011/2012 relative to a bug in clang++ which I believe would have been fixed by then).
#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;
}