0

Possible Duplicate:
Why doesn't a derived template class have access to a base template class' identifiers?

Translating of the following program

A.h

#ifndef A_H
#define A_H
template <class T>
class A
{
  protected :
    T a;
  public:
    A(): a(0) {}
};
#endif

B.h

#ifndef B_H
#define B_H
template <class T>
class A;

template <class T>
class B: public A <T>
{
  protected:
    T b;

  public:
    B() : A<T>(), b(0) {}
    void test () { b = 2 * a;}   //a was not declared in this scope
};
#endif

causes an error: "a was not declared in this scope". (Netbeans 6.9.1).

But the construction

void test () { b = 2 * this->a;} 

is correct... Where is the problem?

Is it better to use forward declaration or file include directive?

B.h

template <class T>
class A;

vs.

#include "A.h"
Community
  • 1
  • 1
Sancho
  • 21
  • 2
  • 2
  • 1
    Read http://www.comeaucomputing.com/techtalk/templates/#whythisarrow – James McNellis Dec 27 '10 at 16:28
  • Duplicate of [Why doesn't a derived template class have access to a base template class' identifiers?](http://stackoverflow.com/questions/1239908/why-doesnt-a-derived-template-class-have-access-to-a-base-template-class-identi) – James McNellis Dec 27 '10 at 16:29

1 Answers1

0

A<T>::a is a dependent name, so you can't use it unqualified.

Imagine that there was a specialization of A<int> somewhere:

template<> class A<int> { /* no a defined */ };

What should the compiler do now? Or what if A<int>::a was a function instead of a variable?

Qualify your access to a, as you've already discovered this->a, and things will work right.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720