I have a couple of errors, where I think I'm calling a function and supplying some variables by pointer and some by value. However, I'm getting compiler errors because, somehow the call by pointer variables are changed to reference to pointer.

Here is the error

g++ -Wall -c -std=c++11  -I.   -c -o SDL_Lesson2.o SDL_Lesson2.cpp
SDL_Lesson2.cpp: In function ‘int main(int, char**)’:
SDL_Lesson2.cpp:56:42: error: call of overloaded ‘renderTexture(SDL_Texture*&,     
SDL_Renderer*&, int, int)’ is ambiguous
SDL_Lesson2.cpp:56:42: note: candidates are:
In file included from SDL_Lesson2.cpp:8:0:
sdlWrapper.hpp:40:6: note: void renderTexture(SDL_Texture*, SDL_Renderer*, int, int)
sdlWrapper.hpp:54:6: note: void renderTexture(SDL_Texture*, SDL_Renderer*, int, int,    
SDL_Rect*)
SDL_Lesson2.cpp:57:43: error: call of overloaded ‘renderTexture(SDL_Texture*&,    
SDL_Renderer*&, int&, int)’ is ambiguous
SDL_Lesson2.cpp:57:43: note: candidates are:
In file included from SDL_Lesson2.cpp:8:0:
sdlWrapper.hpp:40:6: note: void renderTexture(SDL_Texture*, SDL_Renderer*, int, int)
sdlWrapper.hpp:54:6: note: void renderTexture(SDL_Texture*, SDL_Renderer*, int, int,    
SDL_Rect*)

The code that is at these lines is:

SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 
    SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* background = loadTexture("./background.bmp", renderer);
SDL_Texture* image = loadTexture("./image.bmp", renderer);
...
int bW, bH;
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
renderTexture(background, renderer, 0, 0);
renderTexture(background, renderer, bW, 0);

So, I'm wondering, why is the call ambiguous. In my mind, renderTexture(background, renderer, 0, 0) is clearly renderTexture(SDL_Texture*, SDL_Renderer*, int, int). I'm wrong, but I can't figure out why.

Also, between the two lines the first int changes from calling by value to call by reference. That is also a mystery to me.

I believe the problem comes from two overloaded versions.

void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h);

and

void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst,
SDL_Rect *clip = nullptr);

These versions, don't look the same to me. But because SDL_Rect is just four ints in a struct I can see how they might be confused for each other by the compiler.

Should I just remoce one of these functions? Or does the problem lie elsewhere, and I'd just be hiding the problem by removing one of the functions?

有帮助吗?

解决方案

With regard to the second question, the second call registers as an int& because it has the option of fulfilling that signature. Literals can only be passed by value, whereas variables can be passed by reference or value. Thus 0 can only match signatures which take int whereas bW can match signatures which take int or int&.

Regarding the first question, are you sure you've copied those two lines exactly out of sdlWrapper.hpp? It doesn't look like the candidate signatures match the ones you've provided.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top