By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

problems with template iterator

P: n/a
Hi,

I have a small problem with creating a container class using std::list as
the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;
public:
bool isValid() const
{
for( cIt it = mList.begin(); it != mList.end() ; ++it)
dosomething();
}
};

Now my compiler (gcc4) doesn't like the part "cIt it = ", he tells me that
he'd like to have a ";" after cIt:

datalist.cpp:27: error: expected `;' before 'it'

What am I doing wrong here?

Andreas

Nov 4 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de
Hi,

I have a small problem with creating a container class using
std::list as the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;
Change this to

typedef typename std::list<T>::const_iterator cIt;
public:
bool isValid() const
{
for( cIt it = mList.begin(); it != mList.end() ; ++it)
dosomething();
}
};

Now my compiler (gcc4) doesn't like the part "cIt it = ", he tells me
that he'd like to have a ";" after cIt:

datalist.cpp:27: error: expected `;' before 'it'

What am I doing wrong here?


See above. You also haven't defined dosomething() or given isValid a return
value, but I presume that it is done in your real code.
--
John Carson
Nov 4 '05 #2

P: n/a
Andreas Pakulat wrote:
I have a small problem with creating a container class using std::list as
the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;
You lost 'typedef':

typedef typename std::list<T>::const_iterator cIt;
public:
bool isValid() const
{
for( cIt it = mList.begin(); it != mList.end() ; ++it)
dosomething();
You lost the 'return' statement here.
}
};

Now my compiler (gcc4) doesn't like the part "cIt it = ", he tells me that
he'd like to have a ";" after cIt:

datalist.cpp:27: error: expected `;' before 'it'

What am I doing wrong here?


Not much, but enough.

V
Nov 4 '05 #3

P: n/a
John Carson wrote:
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de
Hi,

I have a small problem with creating a container class using
std::list as the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;
Change this to

typedef typename std::list<T>::const_iterator cIt;


Ok, but why does the following not work?

for ( std::list<T>::const_iterator it = mList.begin(); it != mList.end();
++it)
See above. You also haven't defined dosomething() or given isValid a
return value, but I presume that it is done in your real code.


Right, I was just too lazy as neither the return nor dosomething have
anything to do with my problem.

Andreas
Nov 4 '05 #4

P: n/a

"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de...
John Carson wrote:
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de
Hi,

I have a small problem with creating a container class using
std::list as the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;


Change this to

typedef typename std::list<T>::const_iterator cIt;


Ok, but why does the following not work?

for ( std::list<T>::const_iterator it = mList.begin(); it != mList.end();

^
+---- typename

For same reasons as the typedef.

Jeff Flinn
Nov 4 '05 #5

P: n/a
Andreas Pakulat wrote:
John Carson wrote:
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de
Hi,

I have a small problem with creating a container class using
std::list as the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;
Change this to

typedef typename std::list<T>::const_iterator cIt;

Ok, but why does the following not work?

for ( std::list<T>::const_iterator it = mList.begin(); it != mList.end();
++it)


For the same reason you have to use 'typename' in the 'cIt' declaration.
Without 'cIt', you would need to write

for ( typename std::list<T>::const_iterator ...
[...]


V
Nov 4 '05 #6

P: n/a
Victor Bazarov wrote:
Andreas Pakulat wrote:
John Carson wrote:
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de

Hi,

I have a small problem with creating a container class using
std::list as the actual container. Here is what should work:

template <typename T>
class Container
{
std::list<T> mList;
typename std::list<T>::const_iterator cIt;

Change this to

typedef typename std::list<T>::const_iterator cIt;

Ok, but why does the following not work?

for ( std::list<T>::const_iterator it = mList.begin(); it !=
mList.end(); ++it)


For the same reason you have to use 'typename' in the 'cIt' declaration.
Without 'cIt', you would need to write

for ( typename std::list<T>::const_iterator ...


Ok, thanks and be assured I am looking for a C++-book on templates
(unfortunately my c++ book has only a very short chapter).

Andreas

Nov 4 '05 #7

P: n/a
On Fri, 04 Nov 2005 17:20:54 +0100, Andreas Pakulat wrote:
Victor Bazarov wrote:
[..]

for ( typename std::list<T>::const_iterator ...


Ok, thanks and be assured I am looking for a C++-book on templates
(unfortunately my c++ book has only a very short chapter).


You could start by reading the FAQ...

V
Nov 4 '05 #8

P: n/a
"Andreas Pakulat" <ap***@gmx.de> wrote in message
news:43******@news.uni-rostock.de

Ok, thanks and be assured I am looking for a C++-book on templates
(unfortunately my c++ book has only a very short chapter).


http://www.vandevoorde.com/C++Templates/
--
John Carson
Nov 4 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.