2

I’m trying to make a dictionary of fonts with SDL_ttf, just like I made a dictionary with SDL_image. Since fonts are stored with a pnt_size I made a struct containing this info:

    struct fontinfo
    {
        string assetname;
        int size;
    };

Followed by both dictionaries:

map<string, SDL_Surface*> imageDictionary;
map<fontinfo*, TTF_Font*> fontDictionary;

The difference between the two is that the font dictionary not only needs to contain the string to the file but also the size of the font.

Then when a image or font gets requested by an object it calls the get function for it. Now the getSprite works fine:

    SDL_Surface* ResourceManager::getSprite(string assetname)
    {
        if (assetname == "")
        return NULL;

        map<string, SDL_Surface*>::iterator it = imageDictionary.find(assetname);
        if (it != imageDictionary.end())
            return it->second;
        else
        {
            SDL_Surface* image = Load_Image(assetname);
            if (image != NULL)
                imageDictionary.insert(make_pair(assetname, image));
            return image;
        }
    }

The getFont method is almost identical except for the fact that it uses a fontinfo instead of a string:

    TTF_Font* ResourceManager::getFont(string assetname, int size)
    {
        if (assetname == "" || size < 0)
            return NULL;

            fontinfo* info = new fontinfo();

            info->assetname = assetname;
            info->size = size;
            map<fontinfo*, TTF_Font*>::iterator it = fontDictionary.find(info);
            if (it != fontDictionary.end())
                return it->second;
            else
            {
                TTF_Font* font = Load_Font(assetname, size);
                if (font != NULL)
                    fontDictionary.insert(make_pair(info, font));
                return font;
            }
    }

The compiler tells me identifier not found and make_pair is undefined, but only for the make_pair function from getFont. No problems for the make_pair in getSprite.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
M4st3rM1nd
  • 207
  • 3
  • 12
  • Hi me, are these functions in the same file? It would seem like 1 file just doesn't `#include ` and the other does (or includes something else that includes it) – David Apr 29 '13 at 00:16
  • They are indeed in the same file. Both are from the Class ResourceManager and they are right above eachother. – M4st3rM1nd Apr 29 '13 at 00:17

2 Answers2

6

In the first example, you invoke make_pair and the compiler finds it in namespace std because its first argument is in that namespace (std::string). In the second example the compiler has no such reason to look in namespace std, so you must be explicit, and say std::make_pair.

The implicit matching feature is known as ADL or Koenig lookup: https://en.wikipedia.org/wiki/Argument-dependent_name_lookup

As a side note, it is preferable to use myMapType::value_type(foo, bar) rather than std::make_pair(foo, bar) as the argument to std::map::insert. This is because if the types of foo and bar do not exactly match the map's types (including const qualifiers), some compilers will not be able to optimize out the copying of the temporary pair. This is a micro optimization to be sure, so feel free to keep using make_pair if you find it more readable, but be aware.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

just be explicit and state that you want make_pair from std namespace

imageDictionary.insert(std::make_pair(assetname, image));

because in your second function compiler has no means to find it in std since there is no argument from this namespace (then compiler doesn't do a lookup in this namespace to resolve a member)

fontDictionary.insert(std::make_pair(info, font));
4pie0
  • 29,204
  • 9
  • 82
  • 118