【问题标题】:How to overload operators with Boost.Python如何使用 Boost.Python 重载运算符
【发布时间】:2023-03-03 00:07:02
【问题描述】:

我正在尝试使用 Boost.Python 重载 C++ 类的运算符。

根据this,我的做法是正确的……但我有一堆编译器错误。

这是我试图查明问题的一个简单示例:

#include "boost/python.hpp"

using namespace boost::python;

class number
{
public:
    number(int i) : m_Number(i) { }
    number operator+(int i) { return number(m_Number + i); }
private:
    int m_Number;
};

BOOST_PYTHON_MODULE(test)
{
    class_<number>("number", init<int>())
        .def(self() + int());
}

以下是编译器错误:

Error   1   error C2064: term does not evaluate to a function taking 0 arguments    c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp   16  BoostPythonTest
Error   2   error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &,const A3 &)' : expects 5 arguments - 1 provided c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp   16  BoostPythonTest
Error   3   error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &)' : expects 4 arguments - 1 provided    c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp   16  BoostPythonTest
Error   4   error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,A1,const A2 &)' : expects 3 arguments - 1 provided   c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp   16  BoostPythonTest
Error   5   error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,F)' : expects 2 arguments - 1 provided   c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp   16  BoostPythonTest

我在这里错过了什么吗?

谢谢

【问题讨论】:

    标签: c++ operator-overloading boost-python compiler-errors


    【解决方案1】:

    我没有使用 boost.python,但是当某些模板魔术尝试将某些内容绑定到其他内容时,您的错误看起来像是存在一些不兼容的参数。

    所以我查看了您提供的链接,发现了一个主要区别:

    class_<X>("X")
        .def(self + int())
    

    vs 你的

    class_<number>("number", init<int>())
        .def(self() + int());
    

    我猜,selfself() 可以做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多