Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

very wierd error regarding std::map operator

This topic is 3673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a very wierd problem, I got a function to get a tile using my tilemap, defined like this: typedef std::map<vector, Tile*> TileMap; The function is defined as follows:
Tile* TileManager::getTile( vector position )
{
	for (	TileMap::iterator it = m_tiles.begin(); 
		 it != m_tiles.end(); ++it )
	{
              if ( (*it).second->getPosition() == position ) return (*it).second;
	}
	LOG_WARNING( "tile at " << position << " not found, returning null" );
	return NULL;
}
When compiling I get the following error:
Error 65 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const hgeVector' (or there is no acceptable conversion)	c:\program files\microsoft visual studio 8\vc\include\functional 143
The real headache began when I discovered that if I change the return line to this (.second removed):
if ( (*it).second->getPosition() == position ) return (*it);
The error is replaced by another one, the obvious impossible conversion from std::pair to Tile*. Doing the simple
return m_tiles[ position ];
Generates the same error. I been struggling with this behemoth for the past few hours and Im at a total loss! Someone help! Anyone...

Share this post


Link to post
Share on other sites
Advertisement
The error message says that there is no less-then operator for const hgeVectors. I assume hgeVector is your own type, do you provide it with an operator<?

Share this post


Link to post
Share on other sites
How is TileMap defined? How is m_tiles defined? How is vector defined? Which is line 65?

Share this post


Link to post
Share on other sites
Anything you use as a std::map key needs an operator<(). This is because std::map keeps itself sorted, and new values need to know where they belong.

Because std::map is sorted, you can use std::map::find() to look for values in your map.


Tile* TileManager::getTile( vector position )
{
TileMap::iterator it = m_tiles.find(position);
if (it != m_tiles.end())
{
// this iterator points to a std::pair of <key,value>
// the value is in .second
return (*it).second;
}
else
{
// no value was found with this key
LOG_WARNING( "tile at " << position << " not found, returning null" );
return NULL;
}
}




You should not use subscript access (operator[]) here, because if the key you look for does not exist in the map, operator[] will insert a new key into the map with a default constructed value.

What you are doing with the iteration through a sorted key-vale container is very strange, but if you change your typedef to
typedef std::list<std::pair<vector,Tile*> > TileMap
then you can merrily iterate through the list and not worry about the missing vector::operator <(). Depending on what you want this container to do though, this is probably a terrible idea.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!