std :: binary_function - 没有匹配的调用? [英] std::binary_function - no match for call?

查看:135
本文介绍了std :: binary_function - 没有匹配的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#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;
}

,我得到的错误是:

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 只包含参数和返回类型的typedef。它从来不打算作为一个多态基类(即使是,你仍然有问题切片)。

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).

作为一种替代,你可以使用 boost :: function (或 std :: tr1 :: function )像这样:

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>() 
};

这篇关于std :: binary_function - 没有匹配的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆