Reputation: 73376
I have a class Point
, which -for sake of simplicity- looks like this:
template<class DivisionSpace>
class Point {
public:
typedef typename DivisionSpace::FT FT;
const std::vector<FT>* get_coords() const {
return &coords;
}
private:
std::vector<FT> coords;
};
Now, I want in main, to pass the vector of this class to a function which expects
std::vector<FT>& q
. I pass a reference so that I avoid copying. I could use a pointer, but this would mean that I have to change many lines of code (because of the structure of the project).
How can I do it? Is there maybe any "trick" with C++11?
EDIT
Here is the prototype of the function:
void search_nn_prune(std::vector<FT>& q,
std::vector<std::pair<float, int> >& res,
int max_leaf_check, bool sorted_results = false, int k =
1,
float epsilon = 0) {
In main I do
std::vector< Point<Division_space> > q;
std::vector<std::vector<std::pair<float, int> > > results(Q);
for(int i = 0; i < Q; ++i) {
const std::vector<FT>* query = q[i].get_coords();
kdf.search_nn_prune(query, results[i], max_leaf_check, false, k, epsilon);
}
and here is the error
error: no matching function for call to ‘Random_kd_forest<Division_Euclidean_space<int> >::search_nn_prune(const std::vector<int, std::allocator<int> >*&, std::vector<std::pair<float, int> >&, int&, bool, int&, float&)’
note: candidates are:
note: void Random_kd_forest<DivisionSpace>::search_nn_prune(std::vector<typename DivisionSpace::FT>&, std::vector<std::pair<float, int> >&, int, bool, int, float) [with DivisionSpace = Division_Euclidean_space<int>, typename DivisionSpace::FT = int]
note: no known conversion for argument 1 from ‘const std::vector<int, std::allocator<int> >*’ to ‘std::vector<int, std::allocator<int> >&’
note: void Random_kd_forest<DivisionSpace>::search_nn_prune(size_t, std::vector<std::vector<std::pair<float, int> > >&, int, bool, int, float) [with DivisionSpace = Division_Euclidean_space<int>, size_t = unsigned int]
note: no known conversion for argument 2 from ‘std::vector<std::pair<float, int> >’ to ‘std::vector<std::vector<std::pair<float, int> > >&’
Upvotes: 0
Views: 1495
Reputation: 114529
You should declare the function accepting a const
reference to the vector
void search_nn_prune(const std::vector<FT>& q, ...
and the you can pass
kdf.search_nn_prune(*query, ...
(note the *
dereferencing star)
Upvotes: 1
Reputation: 34563
The problem is that your search_nn_prune
function wants a vector<FT>&
argument, but you're passing it a const vector<FT>&
instead. You can't pass a reference to a const
object into a function that wants a modifiable one.
If search_nn_prune
isn't supposed to modify the vector<FT>
it's given, add const
to the argument in the function's declaration. If it is supposed to modify the vector, you'll need to decide on how you want to resolve the situation: Point
doesn't allow changes to the coordinate vector it returns, but you want to pass it to a function that will change the coordinates.
Functions that take arguments by reference should generally take const
references, unless the function is intended to modify the object it's given.
Upvotes: 1