gcc 4 can not resolve member var derived from base class in atemplate class

S

steve yee

#include <iostream>
#include <list>

template<class mt_policy>
class Base
{
public:
int a;
};

template<class mt_policy = int>
class signal0 : public Base<mt_policy>
{
public:
void aaa()
{
std::cout<<"a = "<<a<<std::endl;
}
};

int main()
{
signal0<int> p;
p.aaa();
}
~

test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
test.cpp:17: error: ‘a’ was not declared in this scope
 
A

acehreli

#include <iostream>
#include <list>

template<class mt_policy>
class Base
{
public:
int a;

};

template<class mt_policy = int>
class signal0 : public Base<mt_policy>
{
public:
void aaa()
{
std::cout<<"a = "<<a<<std::endl;

This is the definition of class template signal0, not an instantiation
of it. So the compiler cannot know whether any Base<mt_policy> will
provide an 'a'. For example, some specialization may not have an a.

The compiler cannot assume that a missing name will be available
through a templated base. What if there were two bases?

The solution is to fully qualify 'a': Base said:
}

};

int main()
{
signal0<int> p;
p.aaa();}

~

test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
test.cpp:17: error: ‘a’ was not declared in this scope

Ali
 
J

Juha Nieminen

steve said:
test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
test.cpp:17: error: ‘a’ was not declared in this scope

You have to add this to your signal0 class:

using Base<mt_policy>::a;

I don't remember now why this was standardized like that.
 
B

Bo Persson

Juha said:
VC9 doesn't obey the standard in all respects.

It does respect this, if you ask it too (option /Za). Default is to
compile old, pre-standard, Windows code.


Bo Persson
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top