简体   繁体   中英

How to use a class function in C++?

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 .

将类C定义为“B”以上,或者向前声明它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM