代码之家  ›  专栏  ›  技术社区  ›  wrongusername

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

  •  1
  • wrongusername  · 技术社区  · 15 年前

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

    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]|
    

    我在这里做错什么了?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Wyatt Anderson    15 年前

    你的函数原型被模板化 Data<t> ,看起来您正在对类型为的对象执行此调用 Data<double> 通过一个 std::vector<int> 还有一个 int ,当它可能期望 std::vector<double> 以及 double 对应于 Data 反对。

        2
  •  2
  •   eq-    15 年前

    getPosition 接受三个类型的参数 vector<T> , T bool . 模板类型 T型 在这种情况下 double (如错误消息中所示),但您正在尝试传递 vector<int> int 分别作为第一个和第二个论点。

    可能是 获得位置 不应该模板化?取决于你想要达到的目标-毕竟你有硬编码的int向量。

        3
  •  0
  •   Naveen    15 年前
    vector<T> temp;
    

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

    推荐文章