Help! Error <strange!> with a map-container

Started by
0 comments, last by LeoMaheo 22 years, 7 months ago
Strange thing! I have a bit code that works if I run it in a simple debug-test programme. But when I use the code in the real programe - it hangs up. The programe is kind of an arcade game. I use a map (named "amap")to stoore the location of each "monster" and the players: map. Then I get fast access to the monster using amap.find( position ). When I erase a monster from the map from a position that actually has NO monster - something is erased anyway. Example: I have ONLY a monster at Position(5,5) and I do: amap.erase( Position(30,30) ) - and this results in an erase of something! That''s really strange. But - to make things more strange - the code works (i.e. nothing is erased when nothing should be erased) when I run it in a stand-alone debug programme. The only difference (I think) between the debug-programme and the real one is that the debug-programe uses a much less complex class than CreatureObject to store pointers to in the map. And when I (after erasing the "non-existing" element) trie to iterate through the map the programe get caugth in an infinite loop. I''ve debugged a bit and found out that the "left_link" of the only element present in the map (I ondly did insert one element for easier debugging) refers to the element itself - so it isn''t so strange that the iteration locks up... it keeps going to the same element all the time.... WHY is a non-existing element erased? And why does the element''s left_link refer to the element itself?? If you have any idea of what might be wrong - I would be grateful if you would like to help me. I have no idea how to solve this strange error... (of course I could use some other container than a map - but that seems stupid... I must find the error so I never do it again... hmm...) The code I use to accomplish the error: CreatureObject* ptp5; CreatureObject* ptp6; map amap; // This is the map that contains all monsters (guess you understood that) void print_amap(); //forward void test_testa_actors_map() { ptp5 = new CreatureObject(Position(5,5)); ptp6 = new CreatureObject(Position(6,6)); print_amap();// Printing contents of "amap" to a debug-text-file. This works!! (amap is empty I see later in text-file) : : ... I trie to insert ptp5 in the map and then erase it. : This works! amap is empty after this. All ok I think... : // But now when I insert a new monster... amap.insert( make_pair(ptp6->getPos(), ptp6 )); // (this works) print_amap(); // (ptp6 is in amap now.) // ... strange things happen... First, this works: if ( amap.find(Position(30,30)) != amap.end() ) DebugNsp::println("Hittade element (30,30) i ampa."); else DebugNsp::println("Hittade EJ element (30,30) i ampa."); // The "find" above results in that nothing is found - (30,30) // is empty - the "else" is executed. // .. but now ! the error comes. We know that (30,30) is empty! if ( amap.erase(Positoin(30,30) ) // empty! DebugNsp::println("amap: Erased something at an empty " "position??? what?"); // Executed !!! Why! else DebugNsp::println("Non-existing element wasn''t removed -" "good"); //(<-not executed) // Now something is removed! I have no idea what - or why. // Test to print the map now: print_amap(); // When I now in the print_amap() trie to iterate thwough the // map - the programme hangs up when ++i is done in the for- // loop that iterates through the map. }; //This is how it looks where the programme hangs up. (Copied from // BorlandC++ Builder: "tree.h". while-row->infinite loop) iterator& operator++ () { ... while (!__isNil(__left(node))) node = __left(node); ... return *this; } I like to write games.. and to do real sports.
I like to write games.. and to do real sports.
Advertisement
Now I''ve found out what the error was - now everything works. You don''t need to reply any more... (obviously no one did that anyway... to long question mightbye..?)
The error was as follows, and it took two days to find out...

bool operator< (Position a, Position b)
{
if (a.y < b.y) return true;
if (a.y > b.y) return true;
return a.x < b.x;
};
I like to write games.. and to do real sports.

This topic is closed to new replies.

Advertisement