templates: find the syntax error!

B

Bertwim

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
 
T

Thomas J. Gritzan

Bertwim said:
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
 
M

Michael DOUBEZ

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.
 
B

Bertwim

Michael and Thomas,

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

jaybus56

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top