简体   繁体   English

为什么这是“错误数量的模板参数”?

[英]Why is this 'wrong number of template arguments'?

I am new to c++ and I have a question regarding the following piece of code.我是 C++ 新手,我对以下代码有疑问。 I have created an unordered map with name (which is of string type) as key and a tuple of int and long defined as catInfection .我创建了一个无序映射,名称(字符串类型)作为键,一个 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;
}

However, when I try compiling the code, it returns an error saying但是,当我尝试编译代码时,它返回一个错误说

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

Can anyone explain to me what is wrong?任何人都可以向我解释什么是错的? Thanks so much in advance!!非常感谢提前!

You can't use identifiers as part of a template parameter.您不能将标识符用作模板参数的一部分。 You need to write:你需要写:

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

In this case, clang has a more helpful message:在这种情况下,clang 有一个更有帮助的信息:

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

It's generally helpful to read an error message from a different compiler if an error doesn't make sense.如果错误没有意义,从不同的编译器读取错误消息通常会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM