Reputation: 16651
I'm having a hard time with *
and &
in the following code:
Declare variable and call function:
SDL_Event event;
myfunc(event);
Function:
void MyApp::myfunc(SDL_Event* event) {
while ( SDL_PollEvent(&event) ) {
if (event.type == SDL_QUIT) {
// ...
}
}
}
Compiler: error: no matching function for call to 'MyApp::myfunc(SDL_Event&)' note: candidates are: void MyApp::myfunc(SDL_Event*)|
What am I doing wrong and how can it be fixed?
Upvotes: 1
Views: 429
Reputation: 5963
Two things I see:
First, you're not passing myFunc a pointer, which it expects by your definition. You should either:
Second problem is:
while ( SDL_PollEvent(&event) )
Your parameter called event is a pointer. The Address-of opertor (&) is not necessary to pass the pointer to SDL_PollEvent. Actually, if SDL_PollEvent is expecting a pointer, it's wrong to use address-of in this context. event already IS a pointer. By using address-of, you're saying the address of the pointer, or a pointer to a pointer.
Upvotes: 0
Reputation: 37906
You need to call myfunc with a pointer to a SLD_Event object, instead of calling with the SLD_Event object itself:
myfunc(&event);
Putting a &
in front of a object (or variable) tells the compiler that you want the memory address of the object (which is the so called pointer to it).
Putting a *
in front of a pointer to an object tells the compiler that you want the content of the memory address to which the pointer points (which is the object or variable itself)
The compiler error you get, tells that the compiler is not able to find a function with the name myfunc that accepts a (reference to) an SLD_Event object. Which is correct since the (only) myfunc function available requires a pointer to a SLD_Event object as its argument.
The second (independent to the first one) compiler error (error: cannot convert 'SDL_Event**' to 'SDL_Event*' for argument '1' to 'int SDL_PollEvent(SDL_Event*)'
) you got, tells that the variable type you put into the SLD_PollEvent
function is incompatible with the expected typeand it could not be converted automatically.
Because you added a &
before the event
variable in that function call, you are basically asking to get the pointer to the memory location which points the the event object. The is perfectly legal C code, but not what you want, because the SLD_EventPoll
function expect the pointer the the event object (which has the type SLD_Event *
).
So to solve your second error change the line into:
while ( SDL_PollEvent(event) ) {
(notice the removed &
)
The third error in your code is the event.type
. Because event is a pointer the the SLD_Event
object, C++ expects a ->
instead of the .
(which is used for objects, ie SLD_Event
without the *
).
Upvotes: 5
Reputation: 54806
Your myfunc()
method takes a pointer to an SDL_Event
(i.e. an SDL_Event*
), not an SDL_Event
instance. Try doing:
SDL_Event event;
myfunc(&event);
Also note that unless SDL_PollEvent()
expects a parameter of type SDL_Event**
you do not need the &
there. You can just do:
while ( SDL_PollEvent(event) ) {
if (event->type == SDL_QUIT) {
// ...
}
}
...since you already have a pointer to the SDL_Event
. Also note that I have replaced your event.type
with event->type
(also valid is (*event).type
). Because event
is a pointer at this point, the only way to access the type
field of the actual SDL_Event
instance is to dereference the pointer first. This is what the ->
and *
operators do for you.
Upvotes: 1
Reputation: 3970
you also need to dereference the pointer:
if (event->type == SDL_QUIT) {
// ...
}
and i don't know, but i guess SDL_PollEvent also takes a pointer, so that would be:
SDL_PollEvent(event);
Upvotes: 1
Reputation: 272467
The *
in the function definition means that it takes the address of an SDL_Event
. So you need to give it one:
SDL_Event event;
myfunc(&event);
Upvotes: 4