继承的模板类不会看到其父字段

时间:2016-07-24 18:04:01

标签: c++ oop templates

#include <cstdio>

template<class T>
class A {
    protected:
        T x;
    public:
        A():x(5){}
};

template<class T>
class B:public A<T> {
    public:
        T tellX() {return x;}
};

int main() {
    B<int> cl;
    printf("%d\n",cl.tellX());
    return 0;
}

当我尝试编译它时,我得到:

d:\Docs\work\c++\TEST>g++ main.cpp -std=gnu++11
main.cpp: In member function 'T B<T>::tellX()':
main.cpp:13:21: error: 'x' was not declared in this scope
   T tellX() {return x;}
                     ^

我做错了什么? 如果我删除模板,它工作正常,但与它们一起打破。如何解决?

0 个答案:

没有答案