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

Forward declaration problem with class in class

I am not new to C++ but I still can’t figure the syntax out, the problem is that it’s class in class. I only found answers for normal forward declaration. Please don’t take my code perfection too serious here.

I tried following but nothing worked:

class MyClass {
public:
    class InnerClass
    {
    public:
        void boo()
        {
            foo(*this); // error: foo() is not known
        }
    };
};

void foo(MyClass::InnerClass& ref)
{
}

or

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

class MyClass;
class MyClass::InnerClass; // error: incomplete type is not allowed

void foo(MyClass::InnerClass& ref)
{
}

class MyClass
{
public:
    class InnerClass
    {
    public:
        void boo()
        {
            foo(*this);
        }
    };
};

or

class MyClass
{
public:
    class InnerClass;
};

void foo(MyClass::InnerClass& ref)
{
}

class MyClass // error: redefinition of 'MyClass'
{
public:
    class InnerClass
    {
    public:
        void boo()
        {
            foo(*this);
        }
    };
};

>Solution :

Define MyClass::InnerClass:boo() after foo is defined.

class MyClass {
public:
    class InnerClass
    {
    public:
        void boo();
    };
};

void foo(MyClass::InnerClass& ref)
{
}

void MyClass::InnerClass::boo() {
    foo(*this); 
}
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