2014-04-01 71 views
3

我有几个错误,我认为我在调用一个函数,并通过指针和一些值提供一些变量。但是,我收到编译器错误,因为,通过指针变量调用更改为引用指针。指针调用通过引用成为调用指针

以下是错误

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在结构中只有四个整数,所以我可以看到它们可能如何被编译器混淆。

我应该只是远离其中一个功能?或者问题出在其他地方,我只是通过删除其中一个功能来隐藏问题?

+0

尝试使用'clang ++'编译。它采用与'g ++'相同的参数,但是它的错误消息通常更有用__much__。 –

+0

不确定您显示的原型是否正确。根据错误信息中显示的原型来判断......并从你的猜测中发现,问题在于使用默认参数的renderTexture,当默认参数未指定时与其他版本混淆。 – jsantander

+0

我还建议使用裸C库而不是使用'C++'包装器。您可以使用'std :: unique_ptr'和自定义的'Deleter's'object''C'指针。 –

回答

1

关于第二个问题,第二个调用注册为int &,因为它可以选择满足该签名。文字只能通过值传递,而变量可以通过引用或值传递。因此0只能匹配采用int的签名,而bW可以匹配采用intint&的签名。

关于第一个问题,你确定你已经将这两行完全复制出sdlWrapper.hpp吗?它看起来不像候选人签名与您提供的候选人签名匹配。