template base other template

Discussion in 'C++' started by zdun777, Feb 13, 2011.

  1. zdun777

    zdun777 Guest

    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 ?
     
    zdun777, Feb 13, 2011
    #1
    1. Advertising

  2. On Feb 13, 6:45 pm, zdun777 <> 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
     
    Saeed Amrollahi, Feb 13, 2011
    #2
    1. Advertising

  3. 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.
     
    Alain Ketterlin, Feb 13, 2011
    #3
  4. zdun777

    zdun777 Guest

    Thanks for explanation.
    On 13 Lut, 16:56, Alain Ketterlin <-strasbg.fr> wrote:
    > 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.
     
    zdun777, Feb 13, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tomek
    Replies:
    2
    Views:
    681
    Andrey Tarasevich
    Dec 1, 2003
  2. tomek
    Replies:
    3
    Views:
    751
    David Fisher
    Nov 30, 2003
  3. flopbucket
    Replies:
    2
    Views:
    428
    Andrey Tarasevich
    Jun 23, 2006
  4. nguillot
    Replies:
    5
    Views:
    532
  5. Hicham Mouline
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page