这是我做所有高级操作的主要工人阶级:
class Image
{
public:
Image();
Image(const Image& copy);
~Image();
void loadimage(string filename);
void saveimage(string filename);
Image superimpose(const Image& ontop, Color mask);
int getwidth();
int getheight();
Image operator=(const Image&);
protected:
vector< vector<Color> > pixels;
int width;
int height;
ImageLoader* loader;
};
它有一个复制构造函数:
Image::Image(const Image& copy)
{
width = copy.width;
height = copy.height;
loader = copy.loader;
pixels = copy.pixels;
}
和一个重载的 operator= 方法:
Image Image::operator=(const Image& other)
{
width = other.width;
height = other.height;
loader = other.loader;
pixels = other.pixels
// Return this instance
return *this;
}
析构函数是:
Image::~Image()
{
delete loader;
}
loadimage() 方法创建了一个新的动态分配的加载器来显示:
if(magic_number == "P3")
{
loader = new P3Loader;
}
else if (magic_number == "P6")
{
loader = new P6Loader;
}
else
{
exit(1); // If file is of an inappropriate format
}
当我运行程序时,它挂起。
编辑: 该帖子已被编辑以反映问题。请参阅解决问题的 Praetorian 解决方案。