次のコードに問題があります (コンパイラは文句を言いませんが、実行時にエラー メッセージが表示されます - R6010 中止)。基本的に、画像からデータを読み取り、動的に割り当てられた配列に格納する Image クラスを作成しました。次に、画像データを int main の anther 配列に渡したいと思います。何らかの理由でこれは機能しません。
class Image
{
private:
char* charImage;
int TotRows;
int TotCol;
int Size;
int MaxVal;
char Magic[2];
public:
Image();
Image (const Image& Orig);
~Image();
void operator=(const Image&); //overloaded assignment operator
void ReadImage();
char ReturnImage(int i);
int getSize();
};
Image::Image()//constructor
{
Size = (3 * TotRows * TotCol);
charImage = new char [Size];
}
Image::Image (const Image& Orig)//copy constructor
{
TotRows = Orig.TotRows;
TotCol = Orig.TotCol;
Size = Orig.Size;
charImage = new char [Size];
}
Image::~Image()//destructor
{
delete []charImage;
}
void Image::operator=(const Image& Orig)
{
TotRows = Orig.TotRows;
TotCol = Orig.TotCol;
Size = Orig.Size;
charImage = new char [Size];
for (int i = 0; i < Size; i++)
{
charImage[i]=Orig.charImage[i];
}
}
void Image::ReadImage()
{
//opening original image
ifstream OldImage;
OldImage.open ("image2.ppm", ios::in | ios::binary);
if (!OldImage)
cout << "\nError: Cannot open image file! " << endl;
//reading the header of the original image file
OldImage >> Magic [0] >> Magic [1];
//if the image is not in the right format, do not proceed!
if ((Magic [0] != 'P')||(Magic [1] != '6'))
cout << "\nError: image is in the wrong format!" << endl;
else
OldImage >> TotRows >> TotCol >> MaxVal;
//reading the image in binary format and storing it in the array of characters
OldImage.read(charImage, Size);
}
char Image::ReturnImage(int i)
{
return charImage[i];
}
int Image::getSize()
{
return Size;
}
int main ()
{
char* charImage;
int Size;
Image myImage;
myImage.ReadImage();
Size = myImage.getSize();
charImage= new char [Size];
for (int i=0; i<Size; i++)
{
charImage[i]=myImage.ReturnImage(i);
}
delete [] charImage;
return 0;
}