# very wierd error regarding std::map operator

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

## 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 on other sites
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 on other sites
How is TileMap defined? How is m_tiles defined? How is vector defined? Which is line 65?

##### 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015445
×