【问题标题】:Operator "<<" overflow of doubly linked list. C++操作符“<<”溢出双向链表。 C++
【发布时间】:2019-10-22 20:06:45
【问题描述】:

我无法为我的双向链表创建“

这是我的头文件:


#ifndef SORTEDLIST_H
#define SORTEDLIST_H

#include <iostream>

class SortedList {

private:
    typedef struct node {
        int data;
        node* next;
        node* prev;
    }*nodePtr;

    int theSize;

    nodePtr head;
    nodePtr tail;

public:
    SortedList();
    //~SortedList();
    void insertItem(int inData);
    bool deleteItem(int delData);
    friend ostream& operator <<(ostream& ot, const SortedList& sL);
    int size() const;
    bool empty() const;



};



#endif

这是我的构造函数:

SortedList::SortedList() {
    //Set pointers equal to NULL
    head = NULL;
    tail = NULL;
    theSize = 0;

    head = new node; //create new node of 3 parts: head, data and prev
    tail = new node; //create new node of 3 parts: head, data and prev
    head->next = tail; //next partition points to tail
    head->prev = NULL; //not necessary to put in?
    tail->prev = head; //prev partition points to head
    tail->next = NULL; //not necessary to put in?
    /*temp->next = tail; //access the node the temp pointer is pointing to, set the 'next' part equal to tail*/

}

这是我无法工作的 ostream 重载函数:

ostream& operator<<(ostream& ot, const SortedList& sL)
{
    sL.nodePtr temp;
    temp = sL.head->next;
    while (temp != sL.tail) {
        ot << temp->data << " ";
        temp = temp->next;
    }
    ot << "\n";
}

它一直告诉我 sL.NodePtr、sL.head、sL.tail 无法访问。我确实将它设置为朋友功能,所以我不确定为什么。

【问题讨论】:

  • 你什么都不退货。
  • 那个和return ot;
  • mWebber 如果你是从 Java 或 C# 进来的,C++ 使用 :: 而不是 . 来解析作用域,并且不能从变量中解析作用域,你必须根据它来解析它类型。大多数时候你知道类型,如果你不知道,use decltype
  • 我尝试了您的代码,得到的错误与您不同('ostream' does not name a type,之后修复:invalid use of 'SortedList::nodePtr')。我的结论是您没有提供足够的背景信息;你没有提供minimal reproducible example

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


【解决方案1】:

你的operator&lt;&lt;的实现有几个问题:

  • sL.nodePtr 必须是SortedList::nodePtr

  • while 循环全错了。它不考虑空列表,并且忽略非空列表的tail 节点。哦,等等,您的列表使用 dummy 节点作为其 headtail,这完全没有必要,只会使类的设计复杂化。完全摆脱假人。

  • 它没有return 任何东西。需要返回ot

试试这个:

SortedList::SortedList() {
    //Set pointers equal to NULL
    head = NULL;
    tail = NULL;
    theSize = 0;
}

ostream& operator<<(ostream& ot, const SortedList& sL)
{
    SortedList::nodePtr temp = sL.head;
    while (temp) {
        ot << temp->data << " ";
        temp = temp->next;
    }
    ot << "\n";
    return ot;
}

或者,您可以使用for 循环而不是while 循环:

ostream& operator<<(ostream& ot, const SortedList& sL)
{
    for(SortedList::nodePtr temp = sL.head; temp; temp = temp->next) {
        ot << temp->data << " ";
    }
    ot << "\n";
    return ot;
}

【讨论】:

    【解决方案2】:

    你的操作符重载没有返回任何东西,所以它有undefined behavior

    【讨论】:

    • 可能想回来重新审视这个答案。虽然这是真的,但提问者并没有走得足够远,无法遇到代码中的逻辑错误。他们因编译器错误而停止。
    • 谢谢。原来我的头文件和 .cpp 都没有包含“使用命名空间 std”,所以朋友部分不起作用,因为它认为两个文件中的 operator
    • 然而,尽管在其中添加了命名空间,但 sL.temp 无法正常工作,而 sL.head 可以。 sL 突出显示为“不可修改的 L 值”。
    • @MWeber 见Why is “using namespace std;” considered bad practice?。而且你的SortedList 类中没有temp 成员,所以sL.temp 不存在。
    • 那和using namespace std 放在标题中时可能特别有害。使用您的标头的其他人可能已经编写了他们的代码,他们认为 std 命名空间的内容已被安全地隔离在 std 命名空间内,但是一旦他们包含您的标头,他们的代码就会充满神秘的错误。一旦他们弄清楚了,他们就会来到你的隔间,谈话往往会变得不愉快。
    猜你喜欢
    • 1970-01-01
    • 2013-05-04
    • 2021-05-25
    • 2014-12-09
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多