在C++中使用类外部的指针声明方法

我想在类之外声明以下代码的方法,但是每当该方法是指向私有成员变量的指针时,我都会收到以下错误:“没有函数模板“std::next”的实例与所需的 typeC/C++(386 )”。

class Node
{

private:
    int data;
    Node *next;

public:
    Node() {}

    int GetData() { return data; }
    Node *GetNext() { return next; }

    void SetData(int aData) { data = aData; }
    void SetNext(Node *aNext) { next = aNext; }
};

// outside try class declaration of
int Node::GetData() { return data; }
Node Node::*GetNext() { return next; } // here is the error!!

你能帮我吗?

回答

这是错误的

Node Node::*GetNext() { return next; }

这是对的

Node* Node::GetNext() { return next; }

函数的名称是Node::GetNext和 不是GetNext

  • @zumuha Ask one question per question please.

以上是在C++中使用类外部的指针声明方法的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>