Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates: find the syntax error!

Reply
Thread Tools

templates: find the syntax error!

 
 
Bertwim
Guest
Posts: n/a
 
      12-22-2008
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
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      12-22-2008
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-lit...html#faq-35.18

--
Thomas
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-22-2008
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
 
Reply With Quote
 
Bertwim
Guest
Posts: n/a
 
      12-22-2008
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

 
Reply With Quote
 
jaybus56
Guest
Posts: n/a
 
      12-22-2008
On 22 Dez., 14:42, Bertwim <(E-Mail Removed)> 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
 
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
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments