Reputation: 6744
Inside of a class called Castle I have the following 2 functions:
vector<Location> Castle::getMoves(Location*** squares, Location* loc){
int row = loc->getRow();
int col = loc->getCol();
vector<Location> moves;
//problem is here!
getVerticalMoves(squares, &moves, row, col);
return moves;
}
void Castle::getVerticalMoves(Location*** squares, vector<Location*> * moves, int row, int col){
//need to do something here, but can't call the function!
}
When I try to compile, I get an error that looks like this:
model/src/Castle.cpp: In member function ‘std::vector<Location> Castle::getMoves(Location***, Location*)’:
model/src/Castle.cpp:26:44: error: no matching function for call to ‘Castle::getVerticalMoves(Location***&, std::vector<Location>*, int&, int&)’
model/inc/Castle.h:38:8: note: candidate is: void Castle::getVerticalMoves(Location***, std::vector<Location*>*, int, int)
make: *** [model/obj/Castle.o] Error 1
I do not understand why this error is appearing. Why does it say that I am passing it references?
Upvotes: 0
Views: 72
Reputation: 5973
The issue is you're passing an std::vector* to an std::vector*. Be consistent with your datatypes. If you intend to pass a pointer to a vector of pointers, then pass that.
Upvotes: 0
Reputation: 182827
Do you want a vector of Location
s? Or do you want a vector of pointers to Location
s? You need to pick one and stick with it.
The reason it shows a reference is because if it showed just the value, that would suggest that you could not call a function that takes a reference, since a reference can appear on the left side of an equals sign and a value cannot. What you passed to the function can appear on the left side of an equals sign, so leaving off the & would make the error message less informative. (Suggesting perhaps your mistake was to pass a value where a modifiable reference was required.)
Consider:
int f1(int &j) { j++; return 3; }
int f2(int j) { return j+1; }
int q;
f1(3); // not legal, we are passing an 'int' value and need an 'int' reference
f2(3); // legal, passing an 'int' value function needs an 'int' value
f1(q); // legal, we are passing an 'int&', function need an 'int&'
f2(q); // legal, reference can be demoted to value
So when you pass an lvalue (something that can appear on the left side of an equal sign), you really are calling the function with a reference -- something more powerful than a mere value.
Upvotes: 1
Reputation: 4805
The problem is that your Castle::getVerticalMoves
method takes a vector<Location*>*
as it's second argument, whereas where you're calling it you're passing a vector<Location>*
ie. You're trying to send a vector of Location, to a method that is expecting a vector of pointers to Location.
Upvotes: 1