c++ - 为什么是 'wrong number of template arguments' ?

标签 c++ arguments unordered-map

我是 C++ 新手,我对以下代码有疑问。我创建了一个无序映射,以名称(字符串类型)作为键,并将 int 和 long 的元组定义为 catInfection

#include <bits/stdc++.h>

using namespace std;

typedef tuple<int,long> catInfection; // infection level, "earlyness"

auto comparison_func = [](const pair<string Key,catInfection Value> &A, const pair<string Key,catInfection Value> &B) {
    if (get<0>(A.second) < get<0>(B.second)) {
        return true;
    }
    else if (get<0>(A.second) > get<0>(B.second)) {
        return false;
    }
    else {
        if (get<1>(A.second) < get<1>(B.second)) {
            return false;
        }
        else {
            return true;
        }
    }
}; 

class clinic {
private:
unordered_map <string, catInfection> comp_vector;
.
.
string query() {
   return (*max_element(comp_vector.begin(),comp_vector.end(),comparison_func)).first;
}

但是,当我尝试编译代码时,它返回一个错误

main.cpp:7:67: error: wrong number of template arguments (1, should be 2)
    7 | auto comparison_func = [](const pair<string Key,catInfection Value> &A, const pair<string Key,catInfection Value> &B) {
      |                                                                   ^

谁能给我解释一下这是怎么回事吗?非常感谢!

最佳答案

您不能使用标识符作为模板参数的一部分。您需要写:

auto comparison_func = [](const pair<string,catInfection> &A, 
                          const pair<string,catInfection> &B) 
{
  // ...
}

在这种情况下,clang 有更多 helpful消息:

error: type-id cannot have a name
auto comparison_func = [](const pair<string Key,catInfection Value> &A, const pair<string Key,catInfection Value> &B) {
                                            ^~~

如果错误没有意义,那么从不同的编译器读取错误消息通常会很有帮助。

关于c++ - 为什么是 'wrong number of template arguments' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64213202/

相关文章:

c - 在具有可变参数的函数中传递 "enum"时出错

ios - 如何通过 Hook 类获取参数

c++ - 函数不接受 auto_ptr 的迭代器

c++ - 如何使用 RAII 对套接字建模

c++ - OpenGL 2D 中的可变形地形 [Worms like]

python - 在 python 中,如何将 1 个或多个文件作为具有绝对路径的参数拖放到我的脚本中? (适用于 Windows、Linux 和 Mac)

c++ - 何时检查项目是否已存在于 C++ 集合/映射中?

c++ - 为什么我不能编译一个以一对为键的 unordered_map?

c++ - 配对无序字符串作为 unordered_map 的键

c++ - 对于C++动态内存分配的确切含义有些困惑