gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:57:48 24 4
gpt4 key购买 nike

我是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;
}
但是,当我尝试编译代码时,它返回一条错误消息:
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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com