Template inheritance comile problem

H

Helge

I have a prbl. cannot seem to compile this simple code: (might this be a
bug?)

gcc version 4.1.2

//Begin code

template <class T>
class FirstClass{
public:
T a;
};

template <class T>
class SecondClass : public FirstClass<T>{
public:
T func(){return a;}
};

int main(){}

//End Code

Compiler

test.cpp: In member function ‘T SecondClass<T>::func()’:
test.cpp:10: error: ‘a’ was not declared in this scope
 
G

Gianni Mariani

Helge said:
I have a prbl. cannot seem to compile this simple code: (might this be a
bug?)

gcc version 4.1.2

//Begin code

template <class T>
class FirstClass{
public:
T a;
};

template <class T>
class SecondClass : public FirstClass<T>{
public:
T func(){return a;}

try this line:
T func(){return this->a;}
 
J

John Harrison

Helge said:
I have a prbl. cannot seem to compile this simple code: (might this be a
bug?)

gcc version 4.1.2

//Begin code

template <class T>
class FirstClass{
public:
T a;
};

template <class T>
class SecondClass : public FirstClass<T>{
public:
T func(){return a;}
};

int main(){}

//End Code

Compiler

test.cpp: In member function ‘T SecondClass<T>::func()’:
test.cpp:10: error: ‘a’ was not declared in this scope

Gianni told you the answer, but no it isn't a bug. It's 'two phase
lookup', a complicated subject. Try googling if you want more enlightenment.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top