mercredi 5 février 2020

call of overloaded ‘

I cannot compile the following code.

void print_number(long n) {
   std::cout << n << std::endl;
}

void print_number(float n) {
   std::cout << n << std::endl;
}

void print_pair(std::pair<std::string, long> p) {
   std::cout << std::get<1>(p) << std::endl;
}

void print_pair(std::pair<std::string, float> p) {
   std::cout << std::get<1>(p) << std::endl;
}

int main() {
   print_number(12l);
   print_number(3.4f);

   print_pair({"long", 12l});
   print_pair({"float", 3.4f});

   return 0;
}

print_number functions work well. However, compiler complains about the overloaded print_pair functions: error: call of overloaded ‘print_pair(<brace-enclosed initializer list>)’ is ambiguous.

Are suffices in <brace-enclosed initializer list> or std::pair not working? How can I overload functions receiving a std::pair parameter?

Aucun commentaire:

Enregistrer un commentaire