Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a class function in C++?

Tags:

c++

class

friend

I obtain a error: 'func' does not name a type when a member function func of a class B attempts to return a class C:

class A {
    public:
        class B {
            public:
                C func() const {
                    ...
                }
            private:
                friend class A;
        }

        class C {
            public:
                ...
            private:
                friend class A;
        }
    private:
        ...
}

Whereas, if func is a member function of A, then the following does not produce this error:

class A {
    public:
        class B {
            public:
                ...
            private:
                friend class A;
        }

        C func() const {
            ...
        }

        class C {
            public:
                ...
            private:
                friend class A;
        }
    private:
        ...
}

How can I fix it to make the first version work?

I found a great example here.

like image 864
Pippi Avatar asked Mar 17 '13 05:03

Pippi


1 Answers

Define class C "above" class B, or forward declare it.

like image 182
AndyG Avatar answered Oct 15 '22 14:10

AndyG