Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template base other template

Reply
Thread Tools

template base other template

 
 
zdun777
Guest
Posts: n/a
 
      02-13-2011
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 ?
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      02-13-2011
On Feb 13, 6:45*pm, zdun777 <zdu...@gmail.com> wrote:
> 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
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-13-2011
zdun777 <> writes:

> #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.
 
Reply With Quote
 
zdun777
Guest
Posts: n/a
 
      02-13-2011
Thanks for explanation.
On 13 Lut, 16:56, Alain Ketterlin <al...@dpt-info.u-strasbg.fr> wrote:
> zdun777 <zdu...@gmail.com> writes:
> > #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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
accessing base class members when base is template flopbucket C++ 2 06-23-2006 01:40 AM
Template specialisation of class with base class template ?! Tim Clacy C++ 12 12-03-2003 05:51 PM
Re: template class derived from non-template base class Matt Graham C++ 0 07-21-2003 09:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57