При выполнении сортировки массива объектов своего класса столкнулся с такой проблемой:
Когда забираю сортировки тогда счетчик объектов работает нормально, как только подключаю сортировки в счетчике объектов выпрыгивает число -150 вместо 10, помогите разобраться с проблемой
Код:
class Flower
{
private:
int pelustok;
string month;
static int total;
protected:
string color;
public:
string title;
Flower();
Flower(string tit, string month, string color, int pelustok);
~Flower();
// Çàâàíòàæåííÿ ç ôàéëà
Flower(char *file);
Flower(const Flower& a);
// Çëèâàííÿ äàíèõ ó ôàéë
void SaveToFile(char *fileName);
int GetPelustok(){return pelustok;}
string GetMonth(){return month;}
string GetColor(){return color;}
string Title(){return title;}
static int GetTotalObject(){ return total; }
Flower operator+(Flower obj);
Flower operator=(Flower obj);
bool operator==(Flower obj);
bool operator>(Flower obj);
bool operator<(Flower obj);
friend istream& operator>>(istream& os, Flower& obj);
friend ostream& operator<<(ostream& os, const Flower& obj);
};
int Flower::total = 0;
Flower::~Flower()
{
pelustok = NULL;
month.clear();
color.clear();
title.clear();
total--;
}
Flower::Flower(const Flower& a)
{
pelustok = a.pelustok;
color = a.color;
title = a.title;
month = a.month;
}
Flower::Flower(string tit, string mon, string col, int pelu)
{
pelustok = pelu;
color = col;
title = tit;
month = mon;
total++;
}
Flower::Flower(char *file)
{
ifstream ofs(file);
ofs >> title >> pelustok >> color >> month;
total++;
ofs.close();
}
Flower::Flower()
{
pelustok = 20;
color = "Red";
title = "Roza";
month = "April";
total++;
}
//Îïèñ îïåðàòîðà +
Flower Flower::operator+(Flower obj)
{
Flower tmp;
tmp.pelustok = pelustok += obj.pelustok;
tmp.month = month += obj.month;
tmp.color = color += obj.color;
tmp.title = title += obj.title;
return *this;
}
// Îïèñ îïåðàòîðà ïðèñâîºííÿ
Flower Flower::operator=(Flower obj)
{
pelustok = obj.pelustok;
month = obj.month;
color = obj.color;
title = obj.title;
return *this;
}
// Îïèñ îïåðàòîðà â³äíîøåííÿ "=="
bool Flower::operator==(Flower obj)
{
if((pelustok == obj.pelustok) && (month == obj.month) && (color == obj.color) && (title == obj.title))
{
return true;
}else
{
return false;
}
}
// Îïèñ îïåðàòîðà â³äíîøåííÿ ">"
bool Flower::operator>(Flower obj)
{
if(pelustok > obj.pelustok)
{
return true;
}
return false;
}
// Îïèñ îïåðàòîðà â³äíîøåííÿ "<"
bool Flower::operator<(Flower obj)
{
if(pelustok < obj.pelustok)
{
return true;
}
return false;
}
// Îïèñ îïåðàòîðà êîíñîëüíîãî ââîäó
istream& operator>>(istream& os, Flower& obj)
{
os >> obj.title;
os >> obj.pelustok;
os >> obj.color;
os >> obj.month;
return os;
}
// Îïèñ îïåðàòîðà êîíñîëüíîãî âèâîäó
ostream& operator<<(ostream& os, const Flower& obj)
{
os << obj.title << " " << obj.color << " " << obj.month << " " << obj.pelustok;
return os;
}
int main(int argc, char* argv[])
{
char text[] = "Äî ñîðòóâàííÿ\n";
char text1[] = "ϳñëÿ ñîðòóâàííÿ\n";
CharToOem(text,text);
CharToOem(text1,text1);
Flower obj[10];
ifstream ofs("Flowers1.txt");
cout << text;
for(int i = 0;i < 10;i++)
{
ofs >> obj[i];
cout << obj[i] << endl;
}
Flower tmp;
for (int i = 0,j; i < 10; i++)
{
j = i;
while (j > 0 && obj[j - 1] > obj[j])
{
tmp = obj[j];
obj[j] = obj[j - 1];
obj[j-1] = tmp;
j--;
}
}
cout << text1;
for(int i = 0;i < 10;i++)
{
cout << obj[i] << endl;
}
cout << "Total object in this program " << obj[0].GetTotalObject() << endl;
ofs.close();
getch();
return 0;
}
Часть методов пришлось удалить, форум не позволяет столько инфы отправить