简体   繁体   中英

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 .

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. Ie, 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 *) ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM