no matching function for this function in c++

I'm having trouble with this one. I've read some other pages here but I didn't found a solution for this:

This is my code:

void Tanque::update(World *world, vector<Fantasma*> *ListaFantasmas){

rot = bearing(rect.x, rect.y, ListaFantasmas->back()->rect.x, ListaFantasmas->back()->rect.y);

bala1-> update(&(ListaFantasmas-> back()));
}

the last line gives me an error. (no matchig function for call to bala::update(Fantasma**)

And this is the code for Bala:update:

void bala::update(Fantasma *fantasma){

rect.x = rect.x + speed * cos(rot);
rect.y = rect.y + speed * sin(rot);

rot = bearing(rect.x, rect.y, fantasma -> rect.x, fantasma -> rect.y);

}

Nothing strange as you can see. I'm passing the parameter by reference in the function, I don't see anything wrong.

What do you think the error could be? How can I solve it?

Thanks for reading.

Upvotes: 1

Views: 51

Answers (2)

R Sahu
R Sahu

Reputation: 206567

Problem

The type of ListaFantasmas is vector<Fantasma*> *
Hence, the type of the value returned by ListaFantasmas-> back() is Fantasma*.

The line

bala1-> update(&(ListaFantasmas-> back()));

is equivalent to:

Fantasma* temp = ListaFantasmas-> back();
bala1-> update(&temp);

That means, you are trying to pass a Fantasma** to a function whose argument type is Fantasma*.

Solution

You can use:

bala1-> update(ListaFantasmas-> back());

` To make it more readable, you can use:

Fantasma* temp = ListaFantasmas-> back();
bala1-> update(temp);

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

It seems in this call

bala1-> update(&(ListaFantasmas-> back()));
               ^^^^^^^^^^^^^^^^

the member function back returns a reference to an object of type Fantasma * and you are applying operator & to it getting pointer of type Fantasma **

Maybe you should call the function just like

bala1-> update((ListaFantasmas-> back()));
               ^^^^^^^^^^^^^^^

Upvotes: 2

Related Questions