#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&)’
任何人都可以解释为什么我会收到此错误以及如何摆脱它?
答案 0 :(得分:6)
std::binary_function
仅包含参数和返回类型的typedef。它从来没有打算作为多态基类(即使它是,你仍然有切片问题)。
作为替代方案,您可以像这样使用boost::function
(或std::tr1::function
):
boost::function<double(double, double)> operations[] = {
plus<double>(), minus<double>(), multiplies<double>(), divides<double>()
};