Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Private Struct only returnable with private listed first in a class

So I have run into the case where returning an object of type Node is not allowed if the private variables have been listed after the public as can be seen by the two screenshots below. There CLion is giving me an error as can be seen with Node being red. I understand why this is, however I am wondering if there is anyway to fix this issue without placing/declaring private before public?

Public before private (Desired):
enter image description here

Private before public (what works):
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

The problem is that when the return type Node* is encountered in the member function getCurrentPalyer definition, the compiler doesn’t know that there is a struct named Node. So you’ve to tell that to the compiler which you can do by adding a forward declaration for Node as shown below:

class Palyer 
{ private: struct Node;    //forward declaration added for Node
  public:
    
    Node* func()
    {
        return head;
    }
  private:
    struct Node {};
    Node* head;
};

Working Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading