简体   繁体   中英

Why cant i access subtype via templates?

Here is an example. I get an error on typename T::SubType* inside of the template but not outside.

using gcc0x i get

prog.cpp: In instantiation of 'TemplateBase<A>':
prog.cpp:8:36:   instantiated from here
prog.cpp:4:22: error: invalid use of incomplete type 'class A'
prog.cpp:8:7: error: forward declaration of 'class A'

in vs i get some random error msg. Here is the code.

template< typename T >
struct TemplateBase {
        void ff() {}
        typename T::SubType*f(){ return 0; }
        //typename T::SubType*f();
};

class A : public TemplateBase< A > {
public:
        //struct SubTypeT {
        struct SubType {
        int i;
        };
        //typedef SubTypeT SubType;
private:
        SubType m;
};

template<typename T>
typename T::SubType*f(){ return 0; }

int main() {
        f<A>();
        A a;
        a.ff();
}

A class is fully defined at the closing brace, not at the point where you're listing its parent classes. Therefore, in class A : public TemplateBase< A > { , you're instantiating TemplateBase with an incomplete class A.

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