C++模板没有匹配的函数调用

发布于 2024-10-03 08:43:32 字数 895 浏览 10 评论 0原文

我有一个模板类的成员函数声明如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妥活 2024-10-10 08:43:33

您的函数原型在 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 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.

Hello爱情风 2024-10-10 08:43:33
vector<T> temp;

这里不应该是像 int、double 或 bool 这样的类型吗?

vector<T> temp;

Shouldn't T here be some type like int, double or bool?

夜未央樱花落 2024-10-10 08:43:32

getPosition 接受三个类型为 vectorTbool 的参数。在这种情况下,模板化类型 Tdouble (如错误消息中所示),但您试图传递 vector > 和 int 分别作为第一个和第二个参数。

也许 getPosition 的参数不应该被模板化?取决于您想要实现的目标 - 毕竟您确实有硬编码的 int 向量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文