0
#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;
}

When I try to compile it, I get:

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;}
                     ^

What am I doing wrong? It works fine if I remove templates, but with them it breaks. How to fix it?

Karashevich B.
  • 320
  • 3
  • 14
  • not really a duplicate, it's more like that other question contains an answer itself – Karashevich B. Jul 24 '16 at 18:09
  • 1
    There are many ways to "fix" the problem, as stated in the accepted answer. That answer also explains *why*, which is more important that just saying "use `this->x`". Both questions refer to the same problem, thus they are duplicates – Rakete1111 Jul 24 '16 at 18:12

0 Answers0