Object object = * this seg fault(C ++)

时间:2014-03-16 21:30:06

标签: c++ operator-overloading this

我在下面的代码中遇到了分段错误:

main.cpp

List list;
Movie *m = new Movie();
list+m; //For testing the operator +

编辑:包含所有功能的完整List.cpp

#include "List.h"


using namespace std;



List::List() : head(0),tail(0),size(0) { }

List::~List() 
{
Node *current = head;
while( current != 0 ) {
    Node* next = current->getNext();
    delete current;
    current = next;
}

head = 0;
}

void List::addMovie(Movie *movie)
{
Node *curNode = new Node(movie);

if(head == NULL)
{
    head = curNode;
    tail = curNode;
    curNode->setNext(NULL);
    curNode->setPrevious(NULL);

}

else
{
    curNode->setNext(NULL);
    tail->setNext(curNode);
    curNode->setPrevious(tail);
    tail = curNode;
}

size++;

} 

Node *List::first()
{
return head;
}

Node *List::last()
{
return tail;
}

void List::addMovie(List & list)
{

Node *curNode = list.first();

while(curNode)
{
    addMovie(curNode->getContent());
    curNode = curNode->getNext();
}

}

void List::removeMovie(Movie *movie)
{
if(size == 0) return;
bool found = false;

Node *curNode = head;

while(curNode)
{
    if(curNode->getContent()->getTitle().compare(movie->getTitle()) == 0)
    {
        found = true;
        break;
    }

    curNode = curNode->getNext();
}

if(!found) return;

if(curNode == head && curNode == tail)
{
    head = NULL;
    tail = NULL;
    delete curNode;
    return;
}

if(curNode == head && curNode != tail)
{
    head = curNode->getNext();
    curNode->getNext()->setPrevious(NULL);
    delete curNode;
    return;
}

if(curNode != head && curNode == tail)
{
    tail = curNode->getPrevious();
    curNode->getPrevious()->setNext(NULL);
    delete curNode;
    return;
}

if(curNode != head && curNode != tail)
{
    curNode->getPrevious()->setNext(curNode->getNext());
    curNode->getNext()->setPrevious(curNode->getPrevious());
    delete curNode;
    return;
}


size--;

}
void List::removeMovie(List &list)
{
Node *curNode = list.first();

while(curNode)
{
    removeMovie(curNode->getContent());
    curNode = curNode->getNext();
}
}
int List::getSize()
{
return size;
} 


Movie *List::findMovie(string title)
{
Node *curNode = head;

while(curNode)
{
    if(title.compare(curNode->getContent()->getTitle()) == 0) return curNode->getContent();
    curNode = curNode->getNext();
}

return NULL;
}
void List::clean()
{

head = tail = NULL;
size = 0;
}

void List::operator =(const List& list)
{

head = list.head;
tail = list.tail;
size = list.size;

}

void List::operator +=(Movie *movie)
{
addMovie(movie);
}

void List::operator +=(const List& list)
{
Node *curNode = list.head;

while(curNode)
{
    addMovie(curNode->getContent());
    curNode = curNode->getNext();
}
}

List List::operator +(Movie *m)
{
 cout << "ok" << endl;
    List list = *this;
     cout << "ok" << endl;
    //list += m;
    //Node *f = list.first();
    /*while(f)
    {
        cout << f->getContent()->getTitle() << endl;
        f = f->getNext();
    }*/

    //return list;
    //Crashing after this
}

List List::operator +(const List& list)
{
 List tmp = *this;
 tmp.size++;

 return tmp;
}

我在列表+ m后得到一个seg错误,在调试时得到这个:     没有可用于&#34; libstdc ++ - 6!_ZNKSs7_M_dataEv()的源位于0x6fc5e26c&#34;

0 个答案:

没有答案