【发布时间】: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++ 使用
::而不是.来解析作用域,并且不能从变量中解析作用域,你必须根据它来解析它类型。大多数时候你知道类型,如果你不知道,usedecltype。 -
我尝试了您的代码,得到的错误与您不同(
'ostream' does not name a type,之后修复:invalid use of 'SortedList::nodePtr')。我的结论是您没有提供足够的背景信息;你没有提供minimal reproducible example。
标签: c++ operator-overloading doubly-linked-list ostream