template base other template

Z

zdun777

When I try to compile (g++, ver=4.4.5) this code:

#include <map>
using namespace std;

template<typename T> void fun()
{
map<int, int>::iterator b; //OK
map<T, int>::iterator a; //NOK, error: expected ‘;’ before ‘a’
}

,I get follow compiller error:

error: expected ‘;’ before ‘a’

Can you explain me why I can't define iterator in this way ?
 
S

Saeed Amrollahi

When I try to compile (g++, ver=4.4.5) this code:

#include <map>
using namespace std;

template<typename T> void fun()
{
        map<int, int>::iterator b;      //OK
        map<T, int>::iterator a;       //NOK, error: expected ‘;’ before ‘a’

}

,I get follow compiller error:

error: expected ‘;’ before ‘a’

Can you explain me why I can't define iterator in this way ?

Hi
Under g++ 4.5.0, in addition to above
error message, the compiler issues another message, which describes
and gives hint, what you have to do:
error: need 'typename' before std::map<T, int>::iterator' because
'std::map<T, int>'
is a dependent scope.

HTH
-- Saeed Amrollahi
 
A

Alain Ketterlin

zdun777 said:
#include <map>
using namespace std;

template<typename T> void fun()
{
map<int, int>::iterator b; //OK
map<T, int>::iterator a; //NOK, error: expected ‘;’ before ‘a’
}

,I get follow compiller error:

error: expected ‘;’ before ‘a’

Can you explain me why I can't define iterator in this way ?

The compiler can't guess that "iterator" is a type inside map<T,int>
(you could have a specialization of map for some T where "iterator" is a
static member---and actually that's what the compiler assumes). You have
to say:

typename map<T,int>::iterator a;

-- Alain.
 
Z

zdun777

Thanks for explanation.
The compiler can't guess that "iterator" is a type inside map<T,int>
(you could have a specialization of map for some T where "iterator" is a
static member---and actually that's what the compiler assumes). You have
to say:

    typename map<T,int>::iterator a;

-- Alain.
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top