2

Why is the following code not compiling?

#include <iostream>
template <class T>
class A
{
  public:
  T data;
};

template<typename T>
class B : public A<T>
{
 public:
 void foo(){printf("%d\n", data);}
};

int main() 
{
  B<int> b;
}

Error:

bla.cpp: In member function ‘void B<T>::foo()’:
bla.cpp:14:30: error: ‘data’ was not declared in this scope
void foo(){printf("%d\n", data);}

It seems that the member variable "data" is hidden for some reason.

user1829358
  • 1,041
  • 2
  • 9
  • 19

1 Answers1

0

You can access the base's member variables via this:

this->data;
Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65
  • Thanks this solved the problem, but this feels very odd. I wouldn't have to do this if A and B would not be template classes. Why is this? – user1829358 Jun 06 '18 at 05:23