开发者

std::binary_function - no match for call?

开发者 https://www.devze.com 2022-12-25 17:49 出处:网络
include #include <functional> using namespace std; int main() { binary_function<double, double, double> operations[] = {

include

#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) <开发者_StackOverflow社区< endl;
}

and the error I am getting is:

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

Can anyone explain why I am getting this error and how to get rid of it?


std::binary_function only contains typedefs for argument and return types. It was never intended to act as a polymorphic base class (and even if it was, you'd still have problems with slicing).

As an alternative, you can use boost::function (or std::tr1::function) like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消