Set Class - Copy constructor

I keep running into segmentation faults with my current copy constructor.

1
2
3
4
5
6
7
8
9
Set::Set( const Set & A)
{
   Node * Temp = A.Head->Succ;
   while ((Temp) )
   {
      insert(Temp->Item);
      Temp = Temp->Succ;
   }
}
the head hasn't been set to null.
You probably want to start with temp = A.Head, else you're missing the first item.
That still seg faults.... I have been running some tests and the insert( Temp->Item); makes it seg fault right away, but if i take that out the Temp = Temp->Succ; will run til the end of the set then seg fault...
Did you do this?
1
2
3
4
5
6
7
8
9
10
Set::Set( const Set & A)
{
   head == NULL;
   Node * Temp = A.Head;
   while ((Temp) )
   {
      insert(Temp->Item);
      Temp = Temp->Succ;
   }
}
This is what I got... I needed to make a new Node for the head of the Set, but It still seg faults at the end... any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
Set::Set( const Set & A)
{
   this->Head = new (nothrow) Node;
   this->Head->Succ = NULL;
   Node * Temp = A.Head;

   while (Temp)
   {
      Temp = Temp->Succ;
      insert(Temp->Item);
   }
}
now you're advancing to the next pointer before inserting, meaning when you try to insert NULL->Item.
There is a dummy node at the front of the set. So A.head->Succ really points to the first item in the set. Then i try and insert and it seg faults. The loop seg faults without the insert in there, but right when insert is called it seg faults. I tested the insert function outside of the copy constructor and it seems to work just fine.
Topic archived. No new replies allowed.