我是C++的新手,我在以下类中不断收到此错误消息:
class LinkedList {
class Node *head;
class Node {
Student *student;
Node *next;
Node *prev;
public:
Node(Student *n_student, Node *n_next, Node *n_prev);
~Node();
Student *getStudent() const;
Node *getNext() const;
Node *getPrev() const;
};
public:
LinkedList();
~LinkedList();
void printList();
};
Run Code Online (Sandbox Code Playgroud)
导致错误的方法:
void LinkedList::printList() {
using namespace std;
class Node *p_n;
p_n = head; // ERROR!
while (p_n) {
cout << '[' << (*(*p_n).getStudent()).getId() << ']' << endl;
p_n = (*p_n).getNext();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误信息是
错误:无法在赋值时将"Node*"转换为"LinkedList :: Node*"
我已经尝试将Node转换为LinkedList :: Node但我仍然收到相同的消息.我在Xcode中编译它,不确定是否会导致问题.
知道如何解决这个问题吗?
改变这个:
class Node *head;
Run Code Online (Sandbox Code Playgroud)
进入:
Node *head;
Run Code Online (Sandbox Code Playgroud)
当您在类中声明某个类的字段时,您不需要该class关键字.只是类型名称及其对应的标识符.像这样:
Node *n;
LinkedList *l;
Run Code Online (Sandbox Code Playgroud)
没有class关键字.class关键字仅在您实际声明/定义类时使用.