5

I'm trying to understand whay i get an error on this code: (the error is under g++ unix compiler. VS is compiling OK)

template<class T> class A {
public:
    T t;
public:
    A(const T& t1) : t(t1) {}
    virtual void Print() const { cout<<*this<<endl;}
    friend ostream& operator<<(ostream& out, const A<T>& a) {
            out<<"I'm "<<typeid(a).name()<<endl;
            out<<"I hold "<<typeid(a.t).name()<<endl;
            out<<"The inner value is: "<<a.t<<endl;
            return out;
    }
};

template<class T> class B : public A<T> {
public:
    B(const T& t1) : A<T>(t1) {}
    const T& get() const { return t; }
};

int main() {
    A<int> a(9);
    a.Print();
    B<A<int> > b(a); 
    b.Print();
    (b.get()).Print();  
    return 0;
}

This code is giving the following error:

main.cpp: In member function 'const T& B::get() const':
main.cpp:23: error: 't' was not declared in this scope

It did compiled when i changed the code of B to this:

template<class T> class B : public A<T> {
public:
    B(const T& t1) : A<T>(t1) {}
    const T& get() const { return A<T>::t; }
};

I just cant understand what is the problem with the first code...
It doesn't make sense that i really need to write "A::" every time...

1 Answer 1

7

You can also use this->t to access the base class template member.

In B::get(), the name t is not dependent on the template parameter T, so it is not a dependent name. Base class A<T> is obviously dependent on the template parameter T and is thus a dependent base class. Nondependent names are not looked up in dependent base classes. A detailed description of why this is the case can be found in the C++ FAQ Lite.

Sign up to request clarification or add additional context in comments.

2 Comments

The FAQ is wrong on the reason why "this->" works. Compare it with the Standard (emphasize by me). FAQ: "Since this is always implicitly dependent in a template, this->f is dependent and the lookup is therefore deferred until the template is actually instantiated, at which point all base classes are considered ", Standard: "... the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.". The FAQ says it's found because it's dependent, but that's wrong.
GCC also implements this wrong interpretation, and as a result has bug reports open

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.