(İngilizce'm için üzgünüm, Ukraynalıyım) "Sayılar Yığını" (konsol uygulaması) gerçekleştiren bir program yapıyorum. Stac sildikten sonra dinamik bellek temizlenecek yıkıcı eklemek istiyorum. Ne zaman yıkıcı olmadan benim stac yapmak - all right, eğer ben yıkıcı ekliyorum - error var. Uygulama sona erdiğinde yıkıcı çalıştırılır, ancak program ilk işlevi başlatırken hata olur. Yıkıcı olmadan bu hata bende yok. Yıkımın yorumlandığı kaynak kodum.Bir yıkıcı ekledikten sonra, programın onu çağırmasından önce hatayı aldım
#include<iostream>
#include<ctime>
using namespace std;
struct Oneof
{
int num;
Oneof* next;
};
class Stac
{
private:
Oneof * first;
public:
/*~Stac();*/
Stac();
Stac(Stac &n);
void New(int n); //Adding new element
int Remove(); //Reading last element and removing it
int SetLast(); //Reading last element without removing
void Read(); //Reading all stac
friend bool Eq(Stac a, Stac b); //Equive two another stacs
};
Stac::Stac(){first=NULL;}
Stac::Stac(Stac &n){first=n.first;}
/*Stac::~Stac()
{
while(first!=NULL)
{
Oneof *temp=first;
first=first->next;
delete temp;
}
}
*/
void Stac::New(int n) //Adding new element
{
Oneof* temp=new Oneof;
temp->num=n;
temp->next=first;
first=temp;
}
int Stac::Remove() //Reading last element and removing it
{
int a=first->num;
Oneof *temp=first;
first=first->next;
delete temp;
return a;
}
int Stac::SetLast() //Reading last element without removing
{
return first->num;
}
void Stac::Read() //Reading all stac
{
Oneof* temp=NULL;
Oneof* save=NULL;
save=first;
while(first!=NULL)
{
temp=first;
cout<<temp->num<<" ";
first=temp->next;
}
first=save;
}
bool Eq(Stac a, Stac b) //Equive two another stacs
{
Oneof* tempa=a.first;
Oneof* tempb=b.first;
while(tempa!=NULL && tempb!=NULL)
{
if(tempa->num==tempb->num)
{
tempa=tempa->next;
tempb=tempb->next;
}
else return false;
}
if(tempa==NULL && tempb==NULL)return true;
else return false;
}
int main()
{
Stac a;
srand(time(0));
for(int i=0; i<10; i++)
{
a.New(rand()%100);
}
Stac b(a);
cout<<"Chek equive...\n";
bool equ=Eq(a,b);
if(equ==0)cout<<"First!=Second\n";
else cout<<"First==Second\n";
cout<<"\nReading without removing first number of fisrt stac...\n";
int n=a.SetLast();
cout<<n<<endl;
cout<<"\nReading first Stac...\n";
b.Read();
cout<<"\n\nReading second Stac...\n";
a.Read();
cout<<"\n\nAdding new number and reading first Stac...\n";
b.New(rand());
b.Read();
cout<<"\n\nRemoving number and reading second Stac...\n";
int last=a.Remove();
cout<<last<<endl;
a.Read();
cout<<"\n\nChek equive...\n";
bool equ1=Eq(a,b);
if(equ1==0)cout<<"First!=Second\n\n";
else cout<<"First==Second\n\n";
system("pause");
return 0;
}
Kopyanız-yapıcı tarafından nesneleri geçirmek için öyle Varsayılan kopya yapıcı * (senin gibi) sığ * kopyalama farkı yok. –