std::binary_function - 与调用不匹配?

发布于 2024-08-27 21:19:13 字数 784 浏览 3 评论 0原文

包含

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2024-09-03 21:19:13

std::binary_function 仅包含参数和返回类型的 typedef。它从来没有打算充当多态基类(即使它是,你仍然会遇到切片问题)。

作为替代方案,您可以使用 boost::function (或 std::tr1::function),如下所示:

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

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>() 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文