Ive been having some trouble recently with a copy constructor (related to an assigment operator which is overloaded).... ive been getting segmentation fault, and Im pretty sure that i've narrowed it down to the copy constructor

Anyway, here is my code for the copy constructor:

template <typename L>
    List<L>::List(const List<L>& another)
    {
       first = NULL;
       last= NULL;
       length = 0;
       Node<L>* temp = another.first;
       temp->next=another.first->next;
       int count=0;
       while (count < another.size())
       {
          pushBack(temp->data);
          temp=temp->next;
          count++;
       }
    }

it could also be happening in my operator overloading, and ive been having some trouble with that, but i dont think it is, either way here it is:

template <typename L>
    const List<L> List<L>::operator=(const List<L>& another)
    {
       Node<L>* tempLeft = last;
       tempLeft->previous = last->previous;
       
       Node<L>* tempRight = another.first;
       tempRight->next=another.first->next;
       
       int count = 0;
       if (size() <= another.size())
       {
          while (count < size())
          {
             delete tempLeft;
             tempLeft = tempLeft->previous;
             pushBack (tempRight->data);
             tempRight = tempRight->next;
             count++;
          }
          while (count < another.size())
          {
             pushBack (tempRight->data);
             tempRight = tempRight->next;
             count++;
          }
       }
       else
       {
          while (count < another.size())
          {
             delete tempLeft;
             tempLeft = tempLeft->previous;
             pushBack (tempRight->data);
             tempRight = tempRight->next;
             count++;
          }
          while (count < size())
          {
             delete tempLeft;
             tempLeft = tempLeft->previous;
             count++;
          }
       }
       return *this;
    }

Here is an example of the main function (:

int main()
    {
       cout<<"***Working With A List Of Strings ***"<<endl;
       cout << "it wont even print this";
       List<string> students;
       List<string>::Iterator<string> pos;

any advice will be welcomed, thanks!

I'd suggest removing the overloaded operator= and try it again. Why are you overloading it in the first place?

I'd suggest removing the overloaded operator= and try it again. Why are you overloading it in the first place?

ah! thanks yall! idk what dbg means =( but idk why i didnt try this with = operator earlier, but when i take it out, no more segfault (until a late part, which is apparant where) So that solves where it is. Thank you guys so much!

I overloaded it for other functions that require it =(

thanks again!

This article has been dead for over six months. Start a new discussion instead.