C++嵌套类错误“无法在赋值中转换...”
我是 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();
};
导致错误的方法:
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();
}
}
我收到的错误消息是
错误:无法将“Node*”转换为 赋值中的“LinkedList::Node*”
我尝试将 Node 转换为 LinkedList::Node 但我不断收到相同的消息。我正在 Xcode 中编译它,不确定这是否会导致问题。
知道如何解决这个问题吗?
I'm new to C++ and I keep getting this error message in the following class:
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();
};
The method that causes the error:
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();
}
}
The error message I'm getting is
error: cannot convert 'Node*' to
'LinkedList::Node*' in assignment
I've tried casting Node to LinkedList::Node but I keep getting the same message. I'm compiling it in Xcode, not sure if that causes the problem.
Any idea how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改
为:
当您在类内部声明某个类的字段时,不需要
class
关键字。只是类型名称及其相应的标识符。像这样:没有
class
关键字。class
关键字仅在实际声明/定义类时使用。Change this:
Into this:
When you declare a field of a certain class inside a class, you don't need the
class
keyword. Just the type name and its corresponding identifier. Like this:No
class
keyword.class
keyword is only used when you actually declare/define a class.