ApprenticeHacker
ApprenticeHacker

Reputation: 22011

Template Function error, "no matching function for call to..."

See,I have this code:

template<typename T=int>struct color
{
    T red, green, blue;
    color(T r, T g, T b)
    :red(r), green(g), blue(b)
    {
    }

    #ifdef SDL_BACKEND
    template<typename R,typename S> R map(S surf)
    {
        return SDL_MapRGB(surf->format,red,green,blue);
    }
    #endif /* SDL_BACKEND */
};

and I use it here:

pro::color<int> black(0,0,0);
SDL_FillRect(screen, 0, black.map(screen));

Now here's the error I'm getting:

error: no matching function for call to 'pro::color::map(SDL_Surface*&)'|

I'm not that experienced in templates, so I haven't seen this error before. What exactly is the problem?

NOTE: I didn't tag this with the "SDL" tag, because IMHO this question is more related to templates , the fact that I am using SDL is irrelevant. Also I'm using gcc-4.4x with -std=c++0x.

Upvotes: 1

Views: 1346

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476950

This has nothing to do with meta programming. It's just a matter of using templates correctly. The return type cannot be deduced, so you have to specify it; either in the function or in the template instantiation. I.e., pick one of these two:

// Version #1: Change function definition
template<typename S> Uint32 map(S surf) { return SDL_MapRGB(surf->format,red,green,blue); }


// Version #2: Change invocation
black.map<Uint32>(screen);

(In fact, I don't really understand why you need a template here at all. Why not just make the function into Uint32 map(SDL_Surface *)?)

Upvotes: 4

Related Questions