C++ templates no matching function call
-
26-09-2019 - |
Question
I have a member function of a template class declared as such:
template <class T>
int Data<T>::getPosition(vector<T> stuff, T newStuff, bool ascending)
I call this somewhere with the line
frequencies.insert(frequencies.begin() + getPosition(frequencies, current, ascending),
frequencies[i]);
The variables for that line are declared as:
vector<T> temp;
vector<int> frequencies;
int current = frequency.find(words[i])->second;
However, the call to getPosition
gives this error:
Data.h|158|error: no matching function for call to 'primitives::Data<double>::getPosition(std::vector<int, std::allocator<int> >&, int&, bool&)'|
Data.h|165|note: candidates are: int primitives::Data<T>::getPosition(std::vector<T, std::allocator<_CharT> >, T, bool) [with T = double]|
What am I doing wrong here?
Solution
Your function prototype gets templated on Data<t>
, and it looks like you're performing this call on an object with type Data<double>
and passing a std::vector<int>
and an int
, when it probably expects a std::vector<double>
and a double
to correspond to the initial templated type of the Data
object.
OTHER TIPS
getPosition
takes three arguments of type vector<T>
, T
and bool
. The templated type T
in this case is double
(as is shown in the error message), and yet you are trying to pass vector<int>
and int
as the first and second argument, respectively.
Perhaps the parameters for getPosition
should not be templated? Depends on what you are trying to achieve - you do have hard-coded int-vectors there, after all.
vector<T> temp;
Shouldn't T here be some type like int, double or bool?