473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

problems with template iterator

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
8 12365
"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
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
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

"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
1
by: darkstorm | last post by:
Please check this program...When I compiles it in VC.net, it gives the following error: =============== Common\Lib\PList.h(115): error C2440: '=' : cannot convert from 'ListNode *' to...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
3
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general...
8
by: flamexx7 | last post by:
Can anybody tell me what is wrong with declaration of pointer p ? template<class Tclass Stack { struct Link { T* data; Link* next; Link(T* dat, Link* nxt) : data(dat), next(nxt) {} }* head;...
8
by: Fab | last post by:
All, I need your help understanding why the following code does *NOT* compile with G++ (tested with gcc 3.x and 4.1.x): ---------------------------------------------------------------------...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.