Reputation: 3819
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;
}
Upvotes: 2
Views: 6316
Reputation: 310950
According to the C++ Standard
If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted
That your example would work try the following
Image res = std::move( sample );
Upvotes: 2
Reputation: 1997
Well as the message says - the image class doesn't have a copy constructor. So you can either implement one or if your intention was to test move constructor try this:
int main(int argc, char **argv)
{
Image sample;// = readPPM("./lean.ppm");
Image res = std::move(sample);
return 0;
}
std::move
will cast the sample
to r-value reference, so that it can be used by move-constructor/move-assignment.
Upvotes: 1