templates: find the syntax error!

Discussion in 'C++' started by Bertwim, Dec 22, 2008.

  1. Bertwim

    Bertwim Guest

    Hi,

    I can't understand the compiler's error message in the following
    template definition:

    In a header file (.h), I have declared
    ==================================
    template <class T>
    class symtblpp
    {
    private:
    typedef std::map< std::string, T > maptype;
    maptype _map;
    public:
    T* lookup( const std::string& );
    };
    ===================================

    In the implementation file (.cpp), the member 'lookup' is defined:
    =================================
    template <class T>
    T* symtblpp<T>::lookup( const std::string& key )
    {
    maptype::iterator it = _map.find( key );
    return &(*it);
    }
    ================================
    The error message is (++, 4.3.1)

    symtblpp.cpp:42: error: expected `;' before ‘it’
    symtblpp.cpp:45: error: ‘it’ was not declared in this scope

    where 'line 42' refers to the line with the iterator declaration.
    Can somebody explain to me what I do wrong?

    Tanks in advance.
    Bertwim
    Bertwim, Dec 22, 2008
    #1
    1. Advertising

  2. Bertwim schrieb:
    > Hi,
    >
    > I can't understand the compiler's error message in the following
    > template definition:

    [...]
    > In the implementation file (.cpp), the member 'lookup' is defined:
    > =================================
    > template <class T>
    > T* symtblpp<T>::lookup( const std::string& key )
    > {
    > maptype::iterator it = _map.find( key );


    Insert "typename" keyword in front of this line.

    > return &(*it);


    Is there a reason for the parentheses?

    > }
    > ================================
    > The error message is (++, 4.3.1)
    >
    > symtblpp.cpp:42: error: expected `;' before ‘it’
    > symtblpp.cpp:45: error: ‘it’ was not declared in this scope
    >
    > where 'line 42' refers to the line with the iterator declaration.
    > Can somebody explain to me what I do wrong?


    http://www.parashift.com/c -faq-lite/templates.html#faq-35.18

    --
    Thomas
    Thomas J. Gritzan, Dec 22, 2008
    #2
    1. Advertising

  3. Bertwim a écrit :
    > Hi,
    >
    > I can't understand the compiler's error message in the following
    > template definition:

    [snip]
    > typedef std::map< std::string, T > maptype;

    [snip]
    > maptype::iterator it = _map.find( key );


    typename maptype::iterator it = ...

    > return &(*it);
    > }
    > ================================
    > The error message is (++, 4.3.1)
    >
    > symtblpp.cpp:42: error: expected `;' before ‘it’
    > symtblpp.cpp:45: error: ‘it’ was not declared in this scope
    >
    > where 'line 42' refers to the line with the iterator declaration.
    > Can somebody explain to me what I do wrong?


    typename has to be used whenever a name that depends on a template
    parameter is a type. In your case, iterator is a type that depends on
    map<,T>; so typename is requiered.

    You could also define
    typedef typename maptype::iterator iterator;

    in the class.

    It would save you some typing.

    --
    Michael
    Michael DOUBEZ, Dec 22, 2008
    #3
  4. Bertwim

    Bertwim Guest

    Michael and Thomas,

    Thanks! I never fully understood this typename thing. Now I do.

    Bertwim wrote:
    > Hi,
    >
    > I can't understand the compiler's error message in the following
    > template definition:
    >
    > In a header file (.h), I have declared
    > ==================================
    > template <class T>
    > class symtblpp
    > {
    > private:
    > typedef std::map< std::string, T > maptype;
    > maptype _map;
    > public:
    > T* lookup( const std::string& );
    > };
    > ===================================
    >
    > In the implementation file (.cpp), the member 'lookup' is defined:
    > =================================
    > template <class T>
    > T* symtblpp<T>::lookup( const std::string& key )
    > {
    > maptype::iterator it = _map.find( key );
    > return &(*it);
    > }
    > ================================
    > The error message is (++, 4.3.1)
    >
    > symtblpp.cpp:42: error: expected `;' before ‘it’
    > symtblpp.cpp:45: error: ‘it’ was not declared in this scope
    >
    > where 'line 42' refers to the line with the iterator declaration.
    > Can somebody explain to me what I do wrong?
    >
    > Tanks in advance.
    > Bertwim
    Bertwim, Dec 22, 2008
    #4
  5. Bertwim

    jaybus56 Guest

    On 22 Dez., 14:42, Bertwim <> wrote:
    > Hi,
    >
    > I can't understand the compiler's error message in the following
    > template definition:
    >
    > In a header file (.h), I have declared
    > ==================================
    > template <class T>
    > class symtblpp
    > {
    > private:
    >     typedef std::map< std::string, T > maptype;
    >     maptype _map;
    > public:
    >     T* lookup( const std::string& );};
    >
    > ===================================
    >
    > In the implementation file (.cpp), the member 'lookup' is defined:
    > =================================
    > template <class T>
    > T* symtblpp<T>::lookup( const std::string& key )
    > {
    >     maptype::iterator it = _map.find( key );
    >     return &(*it);}
    >
    > ================================
    > The error message is (++, 4.3.1)
    >
    > symtblpp.cpp:42: error: expected `;' before ‘it’
    > symtblpp.cpp:45: error: ‘it’ was not declared in this scope
    >
    > where 'line 42' refers to the line with the iterator declaration.
    > Can somebody explain to me what I do wrong?
    >
    > Tanks in advance.
    > Bertwim


    Another hint (the typename thing is already explained well): leading
    underscores are reserved for compiler usage. E. g. Andrei Alexandrescu
    prefers trailing underscores for members. I would recommend this, too.
    Or use the MS style "m_" prefix... ;-)

    br
    Juergen
    jaybus56, Dec 22, 2008
    #5
    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. JKop
    Replies:
    3
    Views:
    468
  2. James Aguilar

    Weird syntax error with templates

    James Aguilar, Dec 5, 2004, in forum: C++
    Replies:
    2
    Views:
    475
    Daniel Mitchell
    Dec 5, 2004
  3. Generic Usenet Account
    Replies:
    3
    Views:
    820
    Generic Usenet Account
    Jul 14, 2005
  4. recover
    Replies:
    2
    Views:
    801
    recover
    Jul 25, 2006
  5. Mark Richards
    Replies:
    3
    Views:
    310
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page