list of map iterator

1
2
3
4
5
6
7
8
9
10
11
12
map<int,int> coincidence;
list<map<int,int>> coincidences;
list<map<int,int>>::iterator list_it;
map<int,int>::iterator map_it;

coincidence[0] = 1;
coincidence[1] = 5;
coincidence[2] = 10;
coincidences.push_back(coincidence);

list_it = coincidences.begin();
map_it = (*list_it).begin();


Last line failed due to:
Error: Incorrect referencing of list_it rain.cc:365:
Error: illegal pointer to class object list_it 0x0 2610 rain.cc:365:
*** Interpreter error recovered ***

Where am i wrong?
I'm a little confused I might be missing what your actual problem is? But mine builds and runs with no errors?
g++ 4.3.2 says:

test.cpp:9: error: ‘>>’ should be ‘> >’ within a nested template argument list
test.cpp:10: error: ‘>>’ should be ‘> >’ within a nested template argument list


but if I add spaces as it says, it compiles without error or warning.

What compiler are you using ?
VS 2008 Professional and Express:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <map>
#include <list>
using namespace std;

int main()
{
	map<int,int> coincidence;
	list<map<int,int>> coincidences;

	list<map<int,int>>::iterator list_it;
	map<int,int>::iterator map_it;

	coincidence[0] = 1;
	coincidence[1] = 5;
	coincidence[2] = 10;
	coincidences.push_back(coincidence);

	list_it = coincidences.begin();
	map_it = (*list_it).begin();

	for(; map_it != (*list_it).end(); map_it++)
	{
		cout << "First  = " << map_it->first << endl;
		cout << "Second = " << map_it->second << endl;
	}

	return 0;
}


Works fine no issues.
closed account (z05DSL3A)
In the current standard >> is always parsed as the right shift operator (IIRC, this will be changed in the next standard).

Some tool chains have already implemented this change.
Last edited on
For portability, or more specifically compiler-independence, it is best to use "> >" with a space separating the two closing angle-brackets. This is certainly something to be familiar with, as not all error messages are so explicit.

You could use typedefs in this situation, as well. Here is an example:
1
2
3
4
5
6
7
typedef map<int,int>  CoinMap;
typedef list<CoinMap> ListOfCoins;
// etc...

CoinMap     coincidence;
ListOfCoins coincidences;
// etc... 
Last edited on
Topic archived. No new replies allowed.