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:
air<string,int>("NJ",10));
13 totals.insert(std:
air<string,int>("NY",20));
14 totals.insert(std:
air<string,int>("PA",30));
15 totals.insert(std:
air<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:
ai 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:
air<const int, std ::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&
std::_Rb_tree_iterator<std:
air<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >:
perator=(const
std::_Rb_tree _iterator<std:
air<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:
ai 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>:
perator!=(co nst
std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std:
air<const int,
Can anyone help explain the compile errors/
Jim Anderson
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:
13 totals.insert(std:
14 totals.insert(std:
15 totals.insert(std:
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:
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:
std::char_traits<char>, std::allocator<char> > > >&
std::_Rb_tree_iterator<std:
std::char_traits<char>, std::allocator<char> > > >:
std::_Rb_tree _iterator<std:
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:
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>:
std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std:
7 make: *** [problem.o] Error 1
Can anyone help explain the compile errors/
Jim Anderson