Thread: Problem with map

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    Problem with map

    Wow its been a long while, anyways I am coming back to doing C++ development after doing C# and .net for the last few years and am brushing up on some stuff. I am building a map of verticies and their indicies std:air<Vertex3f, int> and am having a problem. I build my map fine but when i am trying to build a buffer from the indicies in the map i am getting some strange results. The buffer is generated by building the index buffer column by column. The first column everything works great but as soon as i start to build the second column all indicies returned from the map are either 0 or incorrect. Attached is a sample showing this behavoir, for the life of me I cant figure it out.

    BTW
    I am testing this under Visual Studio 2008 TFS & Visual Studio 2010 Beta2 both show the exact same behavoir. This only runs as a release build, there is some problem with the map class in debug, kept failing to insert an item.
    Last edited by xds4lx; 11-27-2009 at 09:21 PM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I guarantee that what it's trying to tell you in the debug build is that it's found the problem for you and you need to pay attention to it to fix the problem.
    You've posted more code than most people will bother trying to look through. (I know I cant be bothered)
    Your better option is to run the debug build and copy and paste the exact error that is reported to you, as well as snippets of bits of your code from what is on the call stack when it breaks into the debugger.
    From there we can probably solve the problem for both debug and release builds.


    Edit:
    Actually wait nevermind. I had a hunch that it's probably a problem with your less than operator which I found in a few seconds in the first source file I opened and I immediately noticed the bug:
    Code:
    bool operator<(const Vertex3f& lhs, const Vertex3f& rhs)
    {
    	if(rhs.X() < lhs.X())
    	{
    		return true;
    	}
    
    	/*if(rhs.Y() < lhs.Y())
    	{
    		return true;
    	}*/
    
    	if(rhs.Z() < lhs.Z())
    	{
    		return true;
    	}
    
    	return false;
    }
    That's an incorrectly implemented less-than operator. Take two verticies of a = (1, 0, 2) and b = (2, 0, 1). By your code a < b and b < a. The way you're written it is a very common mistake, and the debug build was trying to tell you about it.
    The fix is that after comparing one member, you only go on to compare the next member after you've checked that the current one is equal for both sides. Here's one way to fix that:
    Code:
    bool operator<(const Vertex3f& lhs, const Vertex3f& rhs)
    {
    	if(rhs.X() < lhs.X()) return true;
    	if(rhs.X() > lhs.X()) return false;
    	/*
    	if(rhs.Y() < lhs.Y()) return true;
    	if(rhs.Y() > lhs.Y()) return false;
    	*/
    	if(rhs.Z() < lhs.Z()) return true;
    	return false;
    }
    I'd do it as a one-liner myself
    Last edited by iMalc; 11-28-2009 at 01:45 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Wow thanks, been staring at that way too long. Everything works now.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. Hash table - problem
    By Darkinyuasha1 in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2009, 12:11 PM
  3. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  4. problem with a map iterator
    By anykey in forum C++ Programming
    Replies: 17
    Last Post: 04-29-2005, 11:49 PM
  5. my map is showing up 90 degrees turned!
    By frenchfry164 in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2002, 02:32 PM