0

Possible Duplicate:
Inheritance and templates in C++ - why are methods invisible?
Why can't I use variable of parent class that is template class?

I'm trying to use template with inheritance and try to access public member of parent class from child class. But this is not compiling with g++.

template<class T>
class A
{
public:
A<T>* t;
};

template<class E>
class D:public A<E>
{
public:
        D()
        {
                t = 0;
        }
};

int main()
{
        D<int> d;
        return 0;
}

compile error:

g++ Test.C Test.C: In constructor D<E>::D():
      Test.C:27: error: 't' was not declared in this scope
Community
  • 1
  • 1
nsa
  • 199
  • 1
  • 7
  • compile error : g++ Test.C Test.C: In constructor ‘D::D()’: Test.C:27: error: ‘t’ was not declared in this scope – nsa Apr 17 '12 at 08:57
  • Please add the error(s) to your question instead. – Some programmer dude Apr 17 '12 at 09:00
  • have you at least tried to look up "[c++] templates inheritance" or "templates base class member"? – Sebastian Mach Apr 17 '12 at 09:01
  • thanks for the link. i think i missed the A::t = 0; – nsa Apr 17 '12 at 09:01
  • This happens because of the two-phase name lookup (which not all compilers use by default). There are 4 solutions to this problem: **1)** Use the prefix `A::t`, **2)** Use the prefix `this->t`, **3)** Add a statement `using A::t`, **4)** Use a global compiler switch that enables the permissive mode. The pros & cons and details of these solutions are described in https://stackoverflow.com/questions/50321788/a-better-way-to-avoid-public-member-invisibility-and-source-code-bloat-repetitio – George Robinson May 14 '18 at 14:20

0 Answers0