【问题标题】:Why am I getting segmentation fault for when calling templated overloaded operator in c++?为什么在 C++ 中调用模板化重载运算符时出现分段错误?
【发布时间】:2017-11-29 00:53:40
【问题描述】:

我不允许使用 STL 容器!

以下代码中的SortedListClass(const SortedListClass< T > &rhs)operator=(const SortedListClass< T > &rhs)clear() 函数出现分段错误。

这是调试器的结果:

#0 __GI___libc_free (mem=0x4) at malloc.c:2929 #1 0x00000000004016d6 在 SortedListClass::clear (this=0x7ffffffde470) at SortedListClass.inl:45 #2 0x00000000004011b5 在 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 ;
  }

【问题讨论】:

  • 显示的代码不符合stackoverflow.com对minimal reproducible example的要求,因此无法给出权威答案;所以一般的答案“分段错误是因为你的代码中某处的错误”将适用。尽管如此,默认构造函数似乎初始化了一些名为 headtail 的类成员,在复制构造函数中找不到它们。这似乎是非常非常错误的。然后,在 clear(): tempHead = NULL; head = tempHead; 似乎也很错误。这里有太多明显的基本问题。
  • 你的clear 函数完全没有意义。在while 循环的第一次迭代结束时,headtail 都设置为NULL。那么它怎么会循环呢?我同意 Sam 的评论,“这里有太多明显的基本问题。”你只需要找到/修复所有的错误,也许这个项目太雄心勃勃了你的技能水平。
  • 复制构造函数没有初始化 headtail 成员,while() 循环看起来全错了。而operator= 看起来会导致无限递归循环。
  • 无关:template&lt;class T&gt; void SortedListClass&lt; T &gt;::operator=(const SortedListClass&lt; T &gt; &amp;rhs) { this-&gt;clear(); *this = SortedListClass(rhs); } 是末日镇。 *this = SortedListClass(rhs); 将导致无限递归。阅读 stackoverflow.com/questions/3279543/… 以获得更好的路径。
  • headtail的类型是什么?这是否能够在不声明类型的情况下编译?

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


【解决方案1】:

您为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;
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多