为什么在c ++中调用模板化重载运算符时会出现分段错误?

时间:2017-11-29 00:53:40

标签: c++ linked-list operator-overloading

我不允许使用STL容器!

我从以下代码中的SortedListClass(const SortedListClass< T > &rhs)operator=(const SortedListClass< T > &rhs)clear()函数中获取了分段错误。

这是调试器的结果:

#0  __GI___libc_free (mem=0x4) at malloc.c:2929
#1  0x00000000004016d6 in SortedListClass::clear (this=0x7ffffffde470) at SortedListClass.inl:45
#2  0x00000000004011b5 in SortedListClass::operator= (this=0x7ffffffde470, rhs=...) at SortedListClass.inl:28
#3  0x0000000000400c88 in main () at project5.cpp:33

SortedListClass.inl第45行如下:delete tempHead;

SortedListClass.inl第28行如下:this->clear();

project5.cpp第33行如下:copyTestList = testList;

以下是代码:

SortedListClass.inl

template<class T>
SortedListClass< T >::SortedListClass()
{
     head = NULL;
     tail = NULL;
     cout<<"Empty list intialized...."<<endl; 
}

template<class T>
SortedListClass< T >::SortedListClass(const SortedListClass< T > &rhs)
{
    cout<<"Copied Value(s)"<<endl; 
    LinkedNodeClass< T > *rhsFront;
    LinkedNodeClass< T > *newCopyNode;
    rhsFront = rhs.head;
    while(rhsFront != NULL)
    {
        newCopyNode = new LinkedNodeClass< T >(rhsFront,rhsFront->getValue(),
        rhsFront->getNext());
        rhsFront = newCopyNode;
        rhsFront = rhsFront->getNext();
    } 
}

template<class T> 
void SortedListClass< T >::operator=(const SortedListClass< T > &rhs)
{
    this->clear();
    *this = SortedListClass(rhs);
}

template<class T> 
void SortedListClass< T >::clear()
{
    LinkedNodeClass< T >*tempHead;
    LinkedNodeClass< T >*tempTail;
    tempHead = head; 
    tempTail = tail;

    while(tempHead != NULL || tempTail != NULL)
    {
        cout << "Deleting node(s) --> "<< endl;  

        tempHead = head;
        delete tempHead;
        tempHead = NULL; 
        head = tempHead;

        tempTail = tail;
        delete tempTail;
        tempTail = NULL;
        tail = tempTail;

    } 
    cout<<"Deleting complete"<<endl;       
}

project5.cpp

int main()
{

    SortedListClass< int >testList;
    testList.insertValue(3);
    testList.printForward();
    testList.insertValue(4);
    testList.printForward();
    int theVal = 0;
    SortedListClass<int>copyTestList(testList);
    copyTestList = testList;
    copyTestList.getNumElems();
    int index = 0; 
    int outval = 0;
    copyTestList.getElemAtIndex(index,outval);
    copyTestList.printForward();
    copyTestList.removeLast(theVal);
    copyTestList.printBackward();
    return 0 ;
  }

1 个答案:

答案 0 :(得分:0)

您为SortedListClass.inl显示的代码不完整,但您展示的内容都是错误的。尝试更像这样的东西:

template<class T>
SortedListClass<T>::SortedListClass()
    : head(NULL), tail(NULL) /*, init other members as needed ... */
{
     cout << "Empty list initialized...." << endl; 
}

template<class T>
SortedListClass<T>::SortedListClass(const SortedListClass<T> &rhs)
    : head(NULL), tail(NULL) /*, init other members as needed ... */
{
    cout << "Copying value(s)" << endl;  

    LinkedNodeClass<T> *rhsNode = rhs.head;
    while (rhsNode)
    {
        insertValue(rhsNode->getValue());
        rhsNode = rhsNode->getNext();
    } 

    cout << "Copying complete" << endl;
}

template<class T> 
SortedListClass<T>::~SortedListClass()
{
    clear();
}

template<class T> 
SortedListClass<T>& SortedListClass<T>::operator=(const SortedListClass<T> &rhs)
{
    if (&rhs != this)
    {
        SortedListClass<T> tempList(rhs);
        std::swap(head, tempList.head);
        std::swap(tail, tempList.tail);
        // swap other members as needed ...
    }

    return *this;
}

template<class T> 
void SortedListClass<T>::clear()
{
    cout << "Deleting node(s)" << endl;  

    LinkedNodeClass<T> *tempNode = head; 

    head = NULL; 
    tail = NULL; 
    // reset other members as needed ...

    while (tempNode)
    {
        LinkedNodeClass<T> *tempNext = tempNode->getNext();
        delete tempNode;
        tempNode = tempNext;    
    } 

    cout << "Deleting complete" << endl;
}

template<class T> 
void SortedListClass<T>::insertValue(const T &value)
{
    LinkedNodeClass<T> *newNode = new LinkedNodeClass<T>(tail, value, NULL);

    if (!head)
        head = newNode;

    if (tail)
        tail->setNext(newNode);
    tail = newNode;

    // increment size counter, if applicable...
}

template<class T> 
void SortedListClass<T>::removeLast(const T &value)
{
    LinkedNodeClass<T> *tempNode = tail;
    while (tempNode)
    {
        LinkedNodeClass<T> *tempPrevious = tempNode->getPrevious();

        if (tempNode->getValue() == value)
        {
            LinkedNodeClass<T> *tempNext = tempNode->getNext();

            if (tempPrevious)
                tempPrevious->setNext(tempNext);

            if (tempNext)
                tempNext->setPrevious(tempPrevious);

            if (tempNode == tail)
                tail = tempPrevious;

            if (tempNode == head)
                head = tempNext;

            delete tempNode;

            // decrement size counter, if applicable...

            return;
        }

        tempNode = tempPrevious;
    } 
}