我是 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/