为什么是';模板参数的数目错误';? 我是C++新手,我有一个关于下面代码的问题。我创建了一个无序映射,名称(字符串类型)作为键,元组为int,长定义为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; } #包括 使用名称空间std; typedef元组catInfection;//感染水平,“早期” 自动比较_func=[](常量对&A、常量对&B){ 如果(获得(秒)获得(秒)){ 返回false; } 否则{ 如果(获得(秒)

为什么是';模板参数的数目错误';? 我是C++新手,我有一个关于下面代码的问题。我创建了一个无序映射,名称(字符串类型)作为键,元组为int,长定义为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; } #包括 使用名称空间std; typedef元组catInfection;//感染水平,“早期” 自动比较_func=[](常量对&A、常量对&B){ 如果(获得(秒)获得(秒)){ 返回false; } 否则{ 如果(获得(秒),c++,arguments,unordered-map,C++,Arguments,Unordered Map,但是,当我尝试编译代码时,它返回一个错误 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) { |

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

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) {
      |                                                                   ^

main.cpp:7:67:错误:模板参数的数量错误(1,应该是2)
7 |自动比较_func=[](常数对和A、常数对和B){
|                                                                   ^

谁能告诉我怎么回事吗?非常感谢!!

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

auto comparison_func = [](const pair<string,catInfection> &A, 
                          const pair<string,catInfection> &B) 
{
  // ...
}
自动比较函数=[](常量对&A,
康斯特对酒店
{
// ...
}
在本例中,clang有一条更多的消息:

错误:类型id不能有名称
自动比较_func=[](常量对&A、常量对&B){
^~~
如果错误没有意义,从其他编译器读取错误消息通常是有帮助的。

请不要特别是与组合使用。
error: type-id cannot have a name
auto comparison_func = [](const pair<string Key,catInfection Value> &A, const pair<string Key,catInfection Value> &B) {
                                            ^~~