繁体   English   中英

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

[英]How to return nested class pointer from method?

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

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

数据库

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

Databse.cpp中的nextNode方法声明:

Node* Database::Node::nextNode(Node*& start)
{
....
....
    return current->next;
}

谢谢。

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

DataBase::Node* Database::Node::nextNode(Node*& start)
^^^^^^^^^^

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

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

auto Database::Node::nextNode(Node*& start) -> Node* {
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM