Ahoj, program spadne, když se volá destructor ~Image () , konkrétně řádek "delete [] pPoint;"
#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Image
{
public:
Image ();
void CreatePoints (unsigned short int x, unsigned short int y);
~Image ();
bool LoadFile (string nameFile);
bool SaveFile (string nameFile);
unsigned short int GetValueFromThisPoint (unsigned short int itsPoint) const {return pPoint[itsPoint] + itsPoint;}
void ConstructPoint ();
void SetValueOfThisPoint (unsigned short int itsPoint, unsigned short int itsValue) {pPoint[itsPoint] = itsValue;}
void SetPosition(unsigned short int x, unsigned short int y);
private:
unsigned short int *pPoint;
unsigned short int *pHeight, *pWidth;
};
class PlanetaryGeneration
{
public:
PlanetaryGeneration ();
PlanetaryGeneration (unsigned short int x, unsigned short int y);
~PlanetaryGeneration ();
bool SaveFiles ();
bool LoadFiles ();
string GetName(signed short int itsX, signed short int itsY);
void SetCoordinate (signed short int itsX, signed short int itsY);
void SetName ();
private:
Image *pImage;
signed short int *pX;
signed short int *pY;
string *pName;
};
PlanetaryGeneration::PlanetaryGeneration ()
{
pX = new signed short int;
pY = new signed short int;
pName = new string;
*pX = 0;
*pY = 0;
*pName = "Earth";
pImage = new Image[9];
for (int i = 0; i < 9; i++)
{
}
}
PlanetaryGeneration::PlanetaryGeneration (unsigned short int x, unsigned short int y)
{
pX = new signed short int;
pY = new signed short int;
pName = new string;
*pX = 0;
*pY = 0;
*pName = "Earth";
pImage = new Image[9];
for (int i = 0; i < 9; ++i)
{
pImage[i].CreatePoints(x,y);
}
}
PlanetaryGeneration::~PlanetaryGeneration ()
{
delete [] pImage;
delete pX;
delete pY;
delete pName;
}
bool PlanetaryGeneration::SaveFiles ()
{
int k = 0;
string itsName;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
itsName = GetName(i,j);
if (itsName != "error")
{
if (pImage[k].SaveFile(itsName))
{
}
else
{
return false;
}
}
++k;
}
}
return true;
}
bool PlanetaryGeneration::LoadFiles ()
{
int k = 0;
string itsName;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
itsName = GetName(i,j);
if (itsName != "error")
{
if (pImage[k].LoadFile(itsName))
{
}
else
{
return false;
}
}
++k;
}
}
return true;
}
string PlanetaryGeneration::GetName(signed short int itsX, signed short int itsY)
{
if (itsX > 1 || itsX < -1 || itsY > 1 || itsY < -1)
{
cout << " Input method GetName() must be in range -1 to 1!" << endl;
}
else
{
signed short int resultX = *pX + itsX;
signed short int resultY = *pY + itsY;
stringstream streamX;
stringstream streamY;
streamX << resultX;
streamY << resultY;
if (resultX < 0 || resultY < 0)
{}
else
{
string wholeName = *pName + streamX.str() + "_" + streamY.str() + ".raw";
return wholeName;
}
}
return "error";
}
void PlanetaryGeneration::SetCoordinate (signed short int itsX, signed short int itsY)
{
*pX = itsX;
*pY = itsY;
}
void PlanetaryGeneration::SetName ()
{
cout << "Write name of file." << endl;
cin >> *pName;
//cout << endl << *pName << endl;
}
//class Planet;
Image::Image ()
{
pHeight = new unsigned short int;
pWidth = new unsigned short int;
*pHeight = 0;
*pWidth = 0;
pPoint = 0;
}
void Image::CreatePoints (unsigned short x, unsigned short int y)
{
if (x >= 256)
{
if (x <= 4096)
{
*pHeight = x;
}
else
{
*pHeight = 4096;
}
}
else
{
*pHeight = 256;
}
if (y >= 256)
{
if (y <= 4096)
{
*pWidth = y;
}
else
{
*pWidth = 4096;
}
}
else
{
*pWidth = 256;
}
pPoint = new unsigned short int[*pHeight * *pWidth];
ConstructPoint ();
}
Image::~Image ()
{
delete pHeight;
delete pWidth;
delete [] pPoint;
}
bool Image::LoadFile (string nameFile)
{
cout << endl <<"Begin Load File " << "\"" << nameFile << "\"" << "." << endl;
unsigned long int *la = new unsigned long int;
unsigned long int *lb = new unsigned long int;
unsigned long int numberPoint;
unsigned short int number;
*lb = 0;
ifstream inImageFile(nameFile,ios::binary);
if(!inImageFile)
{
cout << endl <<"Unable to open." << endl;
return false;
}
for (unsigned short int i = 0; i < *pHeight; i++)
{
for (unsigned short int j = 0; j < *pWidth; j++)
{
numberPoint = *pHeight*j+ *pWidth;
inImageFile.read((char*) &number,sizeof number);
pPoint[numberPoint] = number;
*la = 100*i/ *pHeight;
if (*la != *lb)
{
cout << "Load File: "<< *la << " %"<< " " << number << endl;
*lb = *la;
}
}
}
inImageFile.close();
cout << endl << "Complete Load File: "<< 100 << " %"<< endl;
return true;
}
bool Image::SaveFile (string nameFile)
{
cout << endl <<"Begin Save File " << "\"" << nameFile << "\"" << "." << endl;
unsigned long int *la = new unsigned long int;
unsigned long int *lb = new unsigned long int;
unsigned long int numberPoint;
unsigned short int number;
*lb = 0;
ofstream outImageFile(nameFile,ios::binary);
if(!outImageFile)
{
cout << endl <<"Unable to open." << endl;
return false;
}
for (unsigned short int i = 0; i < *pHeight; i++)
{
for (unsigned short int j = 0; j < *pWidth; j++)
{
numberPoint = *pHeight*j+ *pWidth;
number = pPoint[numberPoint];
outImageFile.write((char*) &number,sizeof number);
*la = 100*i/ *pHeight;
if (*la != *lb)
{
cout << "Save File: "<< *la << " %"<< endl;
*lb = *la;
}
}
}
outImageFile.close();
cout << endl << "Complete Save File: "<< 100 << " %"<< endl;
return true;
}
void Image::ConstructPoint ()
{
unsigned long int *la = new unsigned long int;
unsigned long int *lb = new unsigned long int;
*lb = 0;
unsigned long int numberPoint;
cout << "Create new image." << endl << endl;
for (unsigned short int i = 0; i < *pHeight; i++)
{
for (unsigned short int j = 0; j < *pWidth; j++)
{
numberPoint = *pHeight*j+ *pWidth;
pPoint[numberPoint] = i;
*la = 100*i/ *pHeight;
if (*la != *lb)
{
cout << "Create: "<< *la << " %"<< " " << pPoint[numberPoint] <<endl;
*lb = *la;
}
}
}
delete la;
delete lb;
cout << "Complete: "<< 100 << " %"<< endl;
}