C++模板没有匹配的函数调用
我有一个模板类的成员函数声明如下:
template <class T>
int Data<T>::getPosition(vector<T> stuff, T newStuff, bool ascending)
我在某处使用该行调用
frequencies.insert(frequencies.begin() + getPosition(frequencies, current, ascending),
frequencies[i]);
该函数 该行的变量声明为:
vector<T> temp;
vector<int> frequencies;
int current = frequency.find(words[i])->second;
但是,对 getPosition 的调用给出了此错误:
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]|
我在做什么这里错了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的函数原型在
Data
上进行模板化,看起来您正在对Data
类型的对象执行此调用并传递std::vector
和一个int
,当它可能需要一个std::vector
和一个double
时> 对应于Data
对象的初始模板化类型。Your function prototype gets templated on
Data<t>
, and it looks like you're performing this call on an object with typeData<double>
and passing astd::vector<int>
and anint
, when it probably expects astd::vector<double>
and adouble
to correspond to the initial templated type of theData
object.这里不应该是像 int、double 或 bool 这样的类型吗?
Shouldn't T here be some type like int, double or bool?
getPosition
接受三个类型为vector
、T
和bool
的参数。在这种情况下,模板化类型T
是double
(如错误消息中所示),但您试图传递vector
> 和int
分别作为第一个和第二个参数。也许
getPosition
的参数不应该被模板化?取决于您想要实现的目标 - 毕竟您确实有硬编码的 int 向量。getPosition
takes three arguments of typevector<T>
,T
andbool
. The templated typeT
in this case isdouble
(as is shown in the error message), and yet you are trying to passvector<int>
andint
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.