473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STL Map iterator compilation error

I am encountering the following issue with STL map iterator - wrapped
within a template. ( I am wrapping it withing a template since I want
to hide the map implementation from the user . At a later point - this
could be a custom hashmap (as opposed to rb-tree map that stl
provides ).

#include <string>
#include <map>
#include <iostream>

template<typename K, typename Vclass TestMap
{

public:
void insert(const std::pair<K, V& ap)
{
data.insert(ap);
}

void list()
{
std::map<K, V>::iterator it = data.begin();
//compiler errors here - error: expected
`;' before "it"

for (; it != data.end(); ++it)
{
std::cout << it->first << " --" << it->second << "\n";
}
}

private:
std::map<K, Vdata;
};

using std::string;

int main()
{
TestMap<std::string, intdata;

data.insert(std::pair<string, int >("Hello", 1));
data.insert(std::pair<string, int >("World", 1));
return EXIT_SUCCESS;
}
The compiler errors out with the following message.

test.cxx:17: error: expected `;' before "it"

Any idea if I am missing anything here.

Nov 29 '07 #1
2 3043
Rakesh Kumar wrote:
I am encountering the following issue with STL map iterator - wrapped
within a template. ( I am wrapping it withing a template since I want
to hide the map implementation from the user . At a later point - this
could be a custom hashmap (as opposed to rb-tree map that stl
provides ).

#include <string>
#include <map>
#include <iostream>

template<typename K, typename Vclass TestMap
{

public:
void insert(const std::pair<K, V& ap)
{
data.insert(ap);
}

void list()
{
typename std::map<K,V>::iterator it = data.begin();

std::map<K,V>::iterator is a dependent name, the compiler needs some
help to know it's a type.
std::map<K, V>::iterator it = data.begin();
//compiler errors here - error: expected
`;' before "it"

for (; it != data.end(); ++it)
{
std::cout << it->first << " --" << it->second << "\n";
}
}

private:
std::map<K, Vdata;
};

using std::string;

int main()
{
TestMap<std::string, intdata;

data.insert(std::pair<string, int >("Hello", 1));
data.insert(std::pair<string, int >("World", 1));
return EXIT_SUCCESS;
}
The compiler errors out with the following message.

test.cxx:17: error: expected `;' before "it"

Any idea if I am missing anything here.
Nov 29 '07 #2
On Nov 28, 4:26 pm, red floyd <no.s...@here.dudewrote:
>
typename std::map<K,V>::iterator it = data.begin();

std::map<K,V>::iterator is a dependent name, the compiler needs some
help to know it's a type.

Thanks redfloyd for the help.

Revised code that compiles:

#include <string>

#include <map>
#include <iostream>

template<typename K, typename Vclass TestMap
{

public:
void insert(const std::pair<K, V& ap)
{
data.insert(ap);
}

void list()
{
typename std::map<K, V>::iterator it = data.begin();
for (; it != data.end(); ++it)
{
std::cout << it->first << " --" << it->second << "\n";
}
}

private:
std::map<K, Vdata;
};

using std::string;

int main()
{
TestMap<std::string, intdata;

data.insert(std::pair<string, int >("Hello", 1));
data.insert(std::pair<string, int >("World", 1));
return EXIT_SUCCESS;
}

Nov 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rex_chaos | last post by:
Hi all, I have designed an iterator and a container like these template <class T> class Iter { ... public: Iter( Container& c, int c ) :cont(c), current(c) T& operator*() {return cont;}
3
by: John Smith | last post by:
Hey I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build fails. Here is the troublesome code:...
13
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my...
7
by: maverick | last post by:
Consider the following class foo.h --------------------------------------------------- #include <list.h> using namespace std ; template < class CFooTempclass CFoo { CFoo(); ~CFoo() ;
2
by: thekestrel | last post by:
Hi, I'm trying to iterate over the values of a list that has a templated type and I'm having trouble with the syntax, could anyone offer somehelp. Simplified version... #include <list>...
5
by: Bill Oliver | last post by:
Help! I am writing an image processing package. For one constructor, I allow creating an image from a map of points and color values. The points are of a "position" class and the colors are...
2
by: hellori | last post by:
Hi all, I have a question about passing iterators to a function. I want to create a function that does something with STL container iterators. The container of the iterator can vary, so I use a...
9
by: subramanian100in | last post by:
Consider the program x.cpp : #include <cstdlib> #include <iostream> #include <list> #include <map> using namespace std; int main()
2
by: Hansel Stroem | last post by:
Is this legitimate STL code ? Compiler seems not to like the iterator ... template <typename Tn> ostream& operator<<(ostream& out, vector<TnVV) { for ( vector<Tn>::iterator it = VV.begin(); it...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.