2

I am new to c++ and I have a question regarding the following piece of code. 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.

#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!!

  • 5
    Please don't [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)especially not in combination with [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – πάντα ῥεῖ Oct 05 '20 at 17:11

1 Answers1

4

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:

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.

cigien
  • 57,834
  • 11
  • 73
  • 112