I have the following two classes: the first one
class node{
public:
node(); //default constructor
node(const node&);//copy constructor
~node(); //destructor
node& operator= (const node&);
short visits;
bool inizpc;
cards cards_first;
cards cards_second;
cards * first; //cards is another class, don't mind that
cards * second;
int * suits;
node * children;
//other members
}
and the class
class tree{
public:
tree() : //initialization list {branch.reserve(20)};//constructor
tree(const tree&); //copy constructor
~tree();//destructor
tree& operator= (const tree&);
std::vector<node> branch;
//other members
void tries(node&);
}
My problem arise when i try to use the branch vector, in particular when i use the .push_back() function inside the following function
void tree::tries(node& a){
a.visits++;
branch.push_back(a);
}
the compiler just terminate the function without running it. The problem is the branch.push_back
but I've not used vectors much so I don't get where the error stand and what i'm missing. Also, how does push_back allocate the object i pass? Does it call the copy constructor?
EDIT: It seems my problem is in the destructor
~node(){
cout << "deleting node\n";
delete[] first;
delete[] second;
delete[] suits;
delete[] children;
}
I tried doing something like
node a;
a.visits=1;
cout << a.visits;
commenting out the destructor definition and declaration it works fine. Is there a reason for it to be deleted before execution ends?
also, here the default constructor, copy constructor and copy assignement definitions, the destructor is as defined above
//default constructor
node::node():
visits(0),
inizpc(false)
cards_first(0,0);//cards have a parameter ctor
cards_second(0,0);
{
first=new carte[10];
second=new carte[10];
suits=new short[4];
children=new node[visits];
}
//copy constructor
node::node(node& a){
visits=a.visits;
inizpc=a.inizpc;
cards_first=a.cards_first;
cards_second=a.cards_second;
first=new cards[10];
for(int i=0; i<10; i++) first[i]=a.first[i];
second=new cards[10];
for (int i=0; i<10; i++) second[i]=a.second[i];
suits=new int[4];
for(int i=0; i<4; i++) suits[i]=a.suits[i];
children=new node[a.visits];
for (int i=0; i<a.visits; i++) children[i]=a.children[i];
}
//assignement
node& node::operator= (node& a){
visits=a.visits;
inizpc=a.inizpc;
cards_first=a.cards_first;
cards_second=a.cards_second;
delete[] first;
first=new cards[10];
for(int i=0; i<10; i++) first[i]=a.first[i];
delete[] second;
second=new cards[10];
for (int i=0; i<10; i++) second[i]=a.second[i];
delete[] suits;
suits=new int[4];
for(int i=0; i<4; i++) suits[i]=a.suits[i];
delete[] children;
children=new node[a.visits];
for (int i=0; i<a.visits; i++) children[i]=a.children[i];
}