Std :: binary_function - no matches to call?

turn on

#include <functional> using namespace std; int main() { binary_function<double, double, double> operations[] = { plus<double>(), minus<double>(), multiplies<double>(), divides<double>() }; double a, b; int choice; cout << "Enter two numbers" << endl; cin >> a >> b; cout << "Enter opcode: 0-Add 1-Subtract 2-Multiply 3-Divide" << endl; cin >> choice; cout << operations[choice](a, b) << endl; } 

and the error I get:

 Calcy.cpp: In function 'int main()': Calcy.cpp:17: error: no match for call to '(std::binary_function<double, double, double>) (double&, double&)' 

Can someone explain why I get this error and how to get rid of it?

+4
source share
1 answer

std::binary_function contains only typedefs for arguments and return types. It was never going to act as a polymorphic base class (and even if that were the case, you would still have problems with slicing).

Alternatively, you can use boost::function (or std::tr1::function ) as follows:

 boost::function<double(double, double)> operations[] = { plus<double>(), minus<double>(), multiplies<double>(), divides<double>() }; 
+6
source

Source: https://habr.com/ru/post/1305865/


All Articles