Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return nested class pointer from method?

I have two classes: Database and Node as a nested class, is it possible to have a method of Node that will return a Node*?

I tried to set the method nextNode return type as Node* but I get a compilation error: '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
};

nextNode method declaration in Databse.cpp:

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

Thanks.

like image 983
Medvednic Avatar asked May 19 '26 06:05

Medvednic


1 Answers

Node is nested in Database, so you need the scope for the return type:

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

The parameter is already in scope, so you may leave it as is.

like image 187
juanchopanza Avatar answered May 20 '26 19:05

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!