std::binary_function - 与调用不匹配?
包含
#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&)’
任何人都可以解释为什么我收到此错误以及如何消除它吗?
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) << 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::binary_function
仅包含参数和返回类型的 typedef。它从来没有打算充当多态基类(即使它是,你仍然会遇到切片问题)。作为替代方案,您可以使用
boost::function
(或std::tr1::function
),如下所示: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
(orstd::tr1::function
) like this: