开发者

C++ nested class error "cannot convert ... in assignment"

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;
    };

pu开发者_开发百科blic:
    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?


Change this:

class Node *head;

Into this:

Node *head;

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:

Node *n;
LinkedList *l;

No class keyword. class keyword is only used when you actually declare/define a class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜