如何从方法返回嵌套类指针?

Med*_*nic 1 c++ linked-list

我有两个类:DatabaseNode为嵌套类,是有可能有一个方法Node,将返回一个Node*

我试图将方法nextNode返回类型设置为Node*但我收到编译错误:'Node' does not name a type

Databse.h

class Database
{
    public:
        Database();
        Database& operator+=(const Client&);
        ~Database();
    private:
        class Node //node as nested class
        {
            public:
            Node(); //ctor
            void setHead(Client*&); //add head node
            Node* nextNode(Node*&); //return new node from the end of he list
            private:
            Client* data; //holds pointer to Client object
            Node* next; //holds pointer to next node in list
        };

        Node *head; //holds the head node
};
Run Code Online (Sandbox Code Playgroud)

nextNode Databse.cpp中的方法声明:

Node* Database::Node::nextNode(Node*& start)
{
....
....
    return current->next;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

jua*_*nza 5

Node是嵌套的Database,因此您需要返回类型的范围:

DataBase::Node* Database::Node::nextNode(Node*& start)
^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

该参数已在范围内,因此您可以保留原样.

  • @Medvednic我会浏览一些[此列表中的书籍](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). (2认同)

Que*_*tin 5

除了juanchopanza的答案,C++ 11的尾随返回类型允许您声明它在范围内:

auto Database::Node::nextNode(Node*& start) -> Node* {
    // ...
}
Run Code Online (Sandbox Code Playgroud)