c++ - Returning a private class that is inside a class -


class linkedlist_movies { private:     class node     {     public:         movie* data;         node* next;                     };      node* head;  public:     linkedlist_movies() { head = null; }     ~linkedlist_movies();     movie* searchbyid(const string& id);     void add(const string& id, const string& name);     void print();     node* findplace(); // <<<<<<<< func  }; 

above linked list in .h file. want return pointer node. in .cpp file, won't let me following:

node* linkedlist_movies::findplace() {  } 

and it's telling me: node undefined.

i know node private. so, how can return it?

you need qualify node, defining function outside of class:

linkedlist_movies::node* linkedlist_movies::findplace() {  } 

note caller of function not able store pointer easily:

linkedlist_movies movies; linkedlist_movies::node* node = movies.findplace(); 

this give compiler error because linkedlist_movies::node private. however, might want.

it is possible them using auto though:

linkedlist_movies movies; auto node = movies.findplace(); 

or, equivalently, when passing deduced template argument:

template <typename t> void foo(t t);  foo(movies.findplace()); 

Comments