1

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

ここで何か不足していますか?

ありがとう

4

1 に答える 1

2

私はboost.pythonを使用していませんが、テンプレートマジックが何かを他のものにバインドしようとすると、互換性のない引数があるように見えます。

だから私はあなたが提供したリンクを見て、1つの大きな違いを見つけました:

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

対あなた

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

私は推測し、selfそれself()を行うことができます。

于 2009-09-09T17:48:02.100 に答える