将 `__str__` 方法添加到 Boost Python C++ 时出现构建问题班级

发布于 2024-09-01 02:22:53 字数 3544 浏览 3 评论 0原文

我开始尝试使用 boost python 并遇到了问题。我尝试将 C++ 类公开给 python,但没有出现任何问题。但我似乎无法在不出现我不明白的构建错误的情况下实现该类的 __str__ 功能。

我正在使用 boostpro 的 boost 1_42 预构建。我使用 cmake 和 vs2010 编译器构建该库。

我有一个非常简单的设置。头文件(tutorial.h)如下所示:

#include <iostream>
namespace TestBoostPython{
    class TestClass {
        private:
            double m_x;
        public:
            TestClass(double x);
            double Get_x() const;
            void Set_x(double x);
    };
    std::ostream &operator<<(std::ostream &ostr, const TestClass &ts);
};

相应的 cpp 文件如下所示:

#include <boost/python.hpp>
#include "tutorial.h"

using namespace TestBoostPython;

TestClass::TestClass(double x)
{
    m_x = x;
}

double TestClass::Get_x() const
{
    return m_x;
}
void TestClass::Set_x(double x)
{
    m_x = x;
}

std::ostream &operator<<(std::ostream &ostr, const TestClass &ts)
{
    ostr << ts.Get_x() << "\n";
    return ostr;
}

BOOST_PYTHON_MODULE(testme)
{
using namespace boost::python;
class_<TestClass>("TestClass", init<double>())
    .add_property("x", &TestClass::Get_x, &TestClass::Set_x)
    .def(str(self))
    ;
}

CMakeLists.txt 如下所示:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

project (testme)

FIND_PACKAGE( Boost REQUIRED )
FIND_PACKAGE( Boost COMPONENTS python REQUIRED )
FIND_PACKAGE( PythonLibs REQUIRED )

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREAD ON)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
INCLUDE_DIRECTORIES ( ${PYTHON_INCLUDE_PATH} )

add_library(testme SHARED tutorial.cpp)
target_link_libraries(testme ${Boost_PYTHON_LIBRARY})
target_link_libraries(testme ${PYTHON_LIBRARY}

我得到的构建错误如下:

Compiling...
tutorial.cpp
    C:\Program Files (x86)\boost\boost_1_42\boost/python/def_visitor.hpp(31) : error C2780: 'void boost::python::api::object_operators::visit(ClassT &,const char *,const boost::python::detail::def_helper &) const' : expects 3 arguments - 1 provided
    with
    [
        U=boost::python::api::object
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/object_core.hpp(203) : see declaration of 'boost::python::api::object_operators::visit'
    with
    [
        U=boost::python::api::object
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/def_visitor.hpp(67) : see reference to function template instantiation 'void boost::python::def_visitor_access::visit,classT>(const V &,classT &)' being compiled
    with
    [
        DerivedVisitor=boost::python::api::object,
        classT=boost::python::class_,
        V=boost::python::def_visitor
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/class.hpp(225) : see reference to function template instantiation 'void boost::python::def_visitor::visit>(classT &) const' being compiled
    with
    [
        DerivedVisitor=boost::python::api::object,
        W=TestBoostPython::TestClass,
        classT=boost::python::class_
    ]
    .\tutorial.cpp(29) : see reference to function template instantiation 'boost::python::class_ &boost::python::class_::def(const boost::python::def_visitor &)' being compiled
    with
    [
        W=TestBoostPython::TestClass,
        U=boost::python::api::object,
        DerivedVisitor=boost::python::api::object
    ]

有谁知道出了什么问题吗?如果我从包装器代码中删除 .def(str(self)) 部分,则一切都可以正常编译,并且该类可以从 python 中使用。如果您能提供帮助,我将非常感激。

谢谢你, Rickard

编辑:忘记了 const

I have started to play around with boost python a bit and ran into a problem. I tried to expose a C++ class to python which posed no problems. But I can't seem to manage to implement the __str__ functionality for the class without getting build errors I don't understand.

I'm using boost 1_42 prebuild by boostpro. I build the library using cmake and the vs2010 compiler.

I have a very simple setup. The header-file (tutorial.h) looks like the following:

#include <iostream>
namespace TestBoostPython{
    class TestClass {
        private:
            double m_x;
        public:
            TestClass(double x);
            double Get_x() const;
            void Set_x(double x);
    };
    std::ostream &operator<<(std::ostream &ostr, const TestClass &ts);
};

and the corresponding cpp-file looks like:

#include <boost/python.hpp>
#include "tutorial.h"

using namespace TestBoostPython;

TestClass::TestClass(double x)
{
    m_x = x;
}

double TestClass::Get_x() const
{
    return m_x;
}
void TestClass::Set_x(double x)
{
    m_x = x;
}

std::ostream &operator<<(std::ostream &ostr, const TestClass &ts)
{
    ostr << ts.Get_x() << "\n";
    return ostr;
}

BOOST_PYTHON_MODULE(testme)
{
using namespace boost::python;
class_<TestClass>("TestClass", init<double>())
    .add_property("x", &TestClass::Get_x, &TestClass::Set_x)
    .def(str(self))
    ;
}

The CMakeLists.txt looks like the following:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

project (testme)

FIND_PACKAGE( Boost REQUIRED )
FIND_PACKAGE( Boost COMPONENTS python REQUIRED )
FIND_PACKAGE( PythonLibs REQUIRED )

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREAD ON)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
INCLUDE_DIRECTORIES ( ${PYTHON_INCLUDE_PATH} )

add_library(testme SHARED tutorial.cpp)
target_link_libraries(testme ${Boost_PYTHON_LIBRARY})
target_link_libraries(testme ${PYTHON_LIBRARY}

The build error I get is the following:

Compiling...
tutorial.cpp
    C:\Program Files (x86)\boost\boost_1_42\boost/python/def_visitor.hpp(31) : error C2780: 'void boost::python::api::object_operators::visit(ClassT &,const char *,const boost::python::detail::def_helper &) const' : expects 3 arguments - 1 provided
    with
    [
        U=boost::python::api::object
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/object_core.hpp(203) : see declaration of 'boost::python::api::object_operators::visit'
    with
    [
        U=boost::python::api::object
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/def_visitor.hpp(67) : see reference to function template instantiation 'void boost::python::def_visitor_access::visit,classT>(const V &,classT &)' being compiled
    with
    [
        DerivedVisitor=boost::python::api::object,
        classT=boost::python::class_,
        V=boost::python::def_visitor
    ]
    C:\Program Files (x86)\boost\boost_1_42\boost/python/class.hpp(225) : see reference to function template instantiation 'void boost::python::def_visitor::visit>(classT &) const' being compiled
    with
    [
        DerivedVisitor=boost::python::api::object,
        W=TestBoostPython::TestClass,
        classT=boost::python::class_
    ]
    .\tutorial.cpp(29) : see reference to function template instantiation 'boost::python::class_ &boost::python::class_::def(const boost::python::def_visitor &)' being compiled
    with
    [
        W=TestBoostPython::TestClass,
        U=boost::python::api::object,
        DerivedVisitor=boost::python::api::object
    ]

Does anyone have any idea on what went wrrong? If I remove the .def(str(self)) part from the wrapper code, everything compiles fine and the class is usable from python. I'd be very greatful for assistance.

Thank you,
Rickard

edit: forgot a const

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

漆黑的白昼 2024-09-08 02:22:53

我最近遇到了这个问题;
有效的解决方案是显式解析这一行上的 strself

.def(str(self))

这样它就变成:

.def(self_ns::str(self_ns::self))

我不知道为什么这是必要的,(知道一些boost python 中发生的重载解析复杂化,可能是那里的东西......)但它对我有用:)

I recently encountered this problem;
The solution that worked was to explicitly resolve the str and self on this line:

.def(str(self))

So that it becomes:

.def(self_ns::str(self_ns::self))

I don't know why this is necessary, (knowing something of the overload-resolution complication that goes on in boost python, it's probably something there...) but it worked for me :)

丢了幸福的猪 2024-09-08 02:22:53

我遇到了同样的事情。添加此行(而不是限定 str 和 self)也有效:

using self_ns::str;

I ran into the same thing. Adding this line (instead of qualifying str and self) also works:

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