problem with iterator (map iterator)

J

Jim Anderson

I'm writing a small program that will use a map (i.e. map<string,int>).
it has been a while since I have written in c++, so I found an example
on the internet and modified it for my own use. But when I try to
compile my program, I get a compile error.

The code looks ok to me, so I did a search on the internet to see if
others had the same problem. I found 3 or 4 examples, but the solutions
were all related to using an iterator, when infact a const_iterator
should be used. That is not the cause of my problem, as near as I can tell.

I have reduced my code to a simple example:

1
2 #include <iostream>
3 #include <map>
4 #include <string>
5
6 using namespace std;
7
8 int main()
9 {
10 map<string, int> totals;
11
12 totals.insert(std::pair<string,int>("NJ",10));
13 totals.insert(std::pair<string,int>("NY",20));
14 totals.insert(std::pair<string,int>("PA",30));
15 totals.insert(std::pair<string,int>("CT",40));
16
17 map<int,string>::iterator iter;
18 for(iter = totals.begin(); iter != totals.end(); ++iter) {
19 cout << (*iter).first << ": " << (*iter).second << endl;
20 }
21 }


I'm using the GNU compiler g++ version 4.4.5 on crunchbang linux. The
compile error messages are:

1 g++ -g -Wall -I/home/jja/bfs/include -DLINUX -c problem.cc
2 problem.cc: In function ‘int main()’:
3 problem.cc:18: error: no match for ‘operator=’ in ‘iter =
totals.std::map<_Key, _Tp, _Compare, _Alloc>::begin [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()’
4 /usr/include/c++/4.4/bits/stl_tree.h:154: note: candidates are:
std::_Rb_tree_iterator<std::pair<const int, std ::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&
std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >::eek:perator=(const
std::_Rb_tree _iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&)
5 problem.cc:18: error: no match for ‘operator!=’ in ‘iter !=
totals.std::map<_Key, _Tp, _Compare, _Alloc>::end [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()’
6 /usr/include/c++/4.4/bits/stl_tree.h:216: note: candidates are:
bool std::_Rb_tree_iterator<_Tp>::eek:perator!=(co nst
std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std::pair<const int,
7 make: *** [problem.o] Error 1


Can anyone help explain the compile errors/

Jim Anderson
 
J

Jim Anderson

RESOLVED.

Please ignore my post. I spent a day looking at this compiler error and
then right after posting, I noticed that line 17 has string and int
transposed and should be:


map<string,int>::iterator iter;

not:

map<int,string>::iterator iter;




I'm writing a small program that will use a map (i.e. map<string,int>).
it has been a while since I have written in c++, so I found an example
on the internet and modified it for my own use. But when I try to
compile my program, I get a compile error.

The code looks ok to me, so I did a search on the internet to see if
others had the same problem. I found 3 or 4 examples, but the solutions
were all related to using an iterator, when infact a const_iterator
should be used. That is not the cause of my problem, as near as I can tell.

I have reduced my code to a simple example:

1
2 #include <iostream>
3 #include <map>
4 #include <string>
5
6 using namespace std;
7
8 int main()
9 {
10 map<string, int> totals;
11
12 totals.insert(std::pair<string,int>("NJ",10));
13 totals.insert(std::pair<string,int>("NY",20));
14 totals.insert(std::pair<string,int>("PA",30));
15 totals.insert(std::pair<string,int>("CT",40));
16
17 map<int,string>::iterator iter;
18 for(iter = totals.begin(); iter != totals.end(); ++iter) {
19 cout << (*iter).first << ": " << (*iter).second << endl;
20 }
21 }


I'm using the GNU compiler g++ version 4.4.5 on crunchbang linux. The
compile error messages are:

1 g++ -g -Wall -I/home/jja/bfs/include -DLINUX -c problem.cc
2 problem.cc: In function ‘int main()’:
3 problem.cc:18: error: no match for ‘operator=’ in ‘iter =
totals.std::map<_Key, _Tp, _Compare, _Alloc>::begin [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()’
4 /usr/include/c++/4.4/bits/stl_tree.h:154: note: candidates are:
std::_Rb_tree_iterator<std::pair<const int, std ::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&
std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >::eek:perator=(const
std::_Rb_tree _iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&)
5 problem.cc:18: error: no match for ‘operator!=’ in ‘iter !=
totals.std::map<_Key, _Tp, _Compare, _Alloc>::end [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()’
6 /usr/include/c++/4.4/bits/stl_tree.h:216: note: candidates are: bool
std::_Rb_tree_iterator<_Tp>::eek:perator!=(co nst
std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std::pair<const int,
std::basic_string<char, std::char_trai ts<char>, std::allocator<char> > >]
7 make: *** [problem.o] Error 1


Can anyone help explain the compile errors/

Jim Anderson
 
J

Jim Anderson

Drew,

Thank you. I spent a day looking at the problem, then 5 minutes after I
posted, I saw the cause. A bit of egg on my face, but sometimes this
happens.

Jim
 
L

Luca Risolia

Jim said:
I noticed that line 17 has string and int
transposed and should be:
map<string,int>::iterator iter;

or:

decltype(totals)::iterator iter;

or, since you use iter in a for loop only:

for (auto iter = std::begin(totals); // or std::cbegin() from C++14
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top