指针调用变为引用指针调用

Call by pointer become call by reference to pointer

本文关键字:指针 调用 引用      更新时间:2023-10-16

>我有几个错误,我认为我正在调用一个函数并通过指针提供一些变量,通过值提供一些变量。但是,我收到编译器错误,因为不知何故,指针变量的调用被更改为对指针的引用。

这是错误

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*)

这些行中的代码是:

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);

所以,我想知道,为什么电话模棱两可。在我看来,renderTexture(background, renderer, 0, 0)显然是renderTexture(SDL_Texture*, SDL_Renderer*, int, int).我错了,但我不知道为什么。

此外,在这两行之间,前int从按值调用更改为按引用调用。这对我来说也是一个谜。

我相信问题来自两个重载版本。

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

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

这些版本,对我来说看起来不一样。但是由于SDL_Rect结构中只有四个整数,我可以看到编译器可能会将它们相互混淆。

我应该取消这些功能之一吗?还是问题出在其他地方,而我只是通过删除其中一个功能来隐藏问题?

关于第二个问题,第二个调用注册为 int&,因为它可以选择完成该签名。 文本只能按值传递,而变量可以按引用或值传递。 因此,0只能匹配需要int的签名,而bW可以匹配需要intint&的签名。

关于第一个问题,你确定你已经完全从sdlWrapper.hpp复制了这两行吗? 候选人签名似乎与您提供的签名不匹配。