Jump to content
  • Advertisement
Sign in to follow this  
FirstStrike

Some silly STL map problem

This topic is 5413 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

typedef std::map<string, TTFont*> FontMap;


{
string key(file);
  TTFont* font = fonts.find(key)->second;
  if(font != fonts.end()){  //<-error here
   ;
  }else{
  font = new TTFont(file, size, defaultColor);
  fonts.insert(key, font);  
  }
  return *font;

}

I get some of these less understandable template-compiler errors. I guess it misses some derefernce or address operator, but can't figure it out... Does anyone see what I can't? Thx

Share this post


Link to post
Share on other sites
Advertisement

typedef std::map<string, TTFont*> FontMap;


{

string key = file;
map<string,TTFont*>::iterator it = fonts.find(key);

TTFont* font;

if(it != fonts.end())
{
font = it->second;
}
else
{
font = new TTFont(file, size, defaultColor);
fonts[key] = font;
}
return *font;

}






that should be closer to what your looking for. it would have been better if you said what you were tying to do and showed the declaration to the function and variables and stuff... but this should be close.

Share this post


Link to post
Share on other sites
I agree, but this:

map<string,TTFont*>::iterator it = fonts.find(key);


should have been written like this:

FontMap::iterator it = fonts.find(key);


Why declare a typedef if you're not going to use it to make your code more readable?

Share this post


Link to post
Share on other sites
Oh, and if you have a reason to use the map's insert method instead of [], you write it like this:

fonts.insert(pair<string, TTFont*>(key, font));


or like this:

fonts.insert(FontMap::value_type(key, font));

Share this post


Link to post
Share on other sites
Hi!
Thx for your suggestions.
Unfortunatley this here isn't correct:

map<string,TTFont*>::iterator it = fonts.find(key);

src/ttfont.cpp:88: error: conversion from `
std::_Rb_tree_iterator<std::pair<const string, TTFont*>, const
std::pair<const string, TTFont*>&, const std::pair<const string,
TTFont*>*>' to non-scalar type `std::_Rb_tree_iterator<std::pair<const
string, TTFont*>, std::pair<const string, TTFont*>&,
std::pair<const string, TTFont*>*>' requested

Share this post


Link to post
Share on other sites
Quote:
Original post by s_p_oneil
Oh, and if you have a reason to use the map's insert method instead of [], you write it like this:

fonts.insert(pair<string, TTFont*>(key, font));


or like this:

fonts.insert(FontMap::value_type(key, font));
or
fonts.insert( make_pair( key, font ) );
:)

Share this post


Link to post
Share on other sites
And just to clarify the original error

Quote:
Original post by FirstStrike

TTFont* font = fonts.find(key)->second;
if(font != fonts.end()){ //<-error here



A standard container's end() function returns an iterator to beyond-the-end. In your case, this is certainly not a TTFont*.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!