Víctor De los Santos Víctor De los Santos - 1 year ago 89
C++ Question

Iterating a map of string and struct throws AccesViolationException in C++/CLI

I am having problems iterating through a AirportLevelMap map with String as a key and a struct than includes a map inside.

typedef struct
std::map<std::string,char> aircraftLevel;
std::string icaoCode;


typedef std::map<std::string,AirportLevelAndPosition> AirportLevelMap;

I am trying to, after the AirportLevelMap has been created and filled, add a value later in each member of the map, by iterating through the whole map. But the iterator seems to not be pointing to each element of the map. In fact, only with a simple cout to check the value of the key (see code below) the system throws an AccessViolationException when debugging.

AirportLevelMap::iterator it;
AirportLevelMap test;
AirportLevelAndPosition aptLevelAndPosition;
char value = '0';
aptLevelAndPosition.icaoCode = "TEST";
for(it = test.begin(); it != test.end(); ++it);
std::cout << test.begin()->first;
std::cout << it->first;

Can't figure out why the iterator is not pointing to the first element of the map, whilst the
std::cout << test.begin()->first
correctly prints the value "TEST".

Thanks in advanced

Answer Source

I assume you actually copied the posted code from your program. In that case, take a closer look at the head of your for loop:

for(it = test.begin(); it != test.end(); ++it);

Erase the trailing semi-colon and you should be fine ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download