我正在使用C ++来实现LinkedList,其他函数和运算符我可以创建Node *就好了。但是当我到达这个运算符“ostream& operator<<(ostream& out,const LinkedList& list)”(输出列表)时,我无法在运算符内部创建临时运算符,可以有谁告诉我导致错误的原因以及如何解决?
这是我的LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
class LinkedList {
typedef struct Node{
int data;
Node* next;
bool operator < (const Node& node) const {
return this->data < node.data;
}
bool operator <= (const Node& node) const {
return this->data <= node.data;
}
bool operator > (const Node& node) const {
return this->data > node.data;
}
bool operator >= (const Node& node) const {
return this->data >= node.data;
}
friend ostream& operator << (ostream& out, const LinkedList& list);
friend istream& operator >> (istream& in, const LinkedList& list);
} * nodePtr;
public:
nodePtr head;
nodePtr curr;
LinkedList();
// functions
void push_front(int);
void push_back(int);
int pop_front();
int pop_back();
int size();
bool contains(int);
void print();
void clear();
// overload
LinkedList& operator =(const LinkedList& list);
bool operator !=(const LinkedList& list) const;
LinkedList operator +(const int v) const;
LinkedList operator +(const LinkedList& list) const;
LinkedList operator - (const int v) const;
friend ostream& operator << (ostream& out, const LinkedList& list);
friend istream& operator >> (istream& in, const LinkedList& list);
};
#endif /* LINKEDLIST_H */
在我的LinkedList.cpp中:
ostream& operator << (ostream& out, const LinkedList& list) {
nodePtr temp = list.head; <----------------- **Unable to resolve identifier nodePtr**
}
我可以在我的其他功能上创建Node *(nodePtr)。
答案 0 :(得分:2)
nodePtr
在LinkedList
内定义,需要合格。改变
nodePtr temp = list.head;
到
LinkedList::nodePtr temp = list.head;