gpt4 book ai didi

c++ - Boost python 和虚拟类

转载 作者:行者123 更新时间:2023-11-28 05:26:12 27 4
gpt4 key购买 nike

我正在努力将一些现有的 C++ 代码暴露给 Python。

我有一个虚拟类:

class SomeVirtualClass {
public:
SomeVirtualClass();
virtual ~SomeVirtualClass();
virtual SomeVirtualClass *clone() const = 0;
virtual SomeVirtualClassType getType() const = 0;
// SomeVirtualClassType is a descriptive enum
virtual std::string getSomeString() const = 0;
private:
//some other things here
}

和一个使用虚拟类型的类:

class A {
public:
SomeVirtualClass getThisThing();
}

我需要能够在 python 中使用 A 中的 getThisThing。该方法可以返回从 SomeVirtualClass 派生的多个类之一。使用 boost 文档和几个类似的 stackoverflow 问题,我想出了:

namespace bp = boost::python
class SomeVirtualClassWrap : public SomeVirtualClass,
public bp::wrapper<SomeVirtualClass> {
public:
std::string getSomeString() {
return this->get_override("getSomeString")();
}

SomeVirtualClass *clone() {
return this->get_override("clone")();
}

SomeVirtualClassType getType() {
return this->get_override("getType")();
}
};

作为虚拟类的包装器并且:

bp::class_ <SomeVirtualClassWrap, boost::noncopyable>("SomeVirtualClass", no_init)
.def("GetSomeString", bp::pure_virtual(&SomeVirtualClass::getSomeString))
.def("Clone", bp::pure_virtual(&SomeVirtualClass::clone))
.def("GetType", bp::pure_virtual(&SomeVirtualClass::getType));

作为python暴露。

供引用 A 类也暴露了:

bp::class_<A>("A") {
.def("GetThisThing", &A::getThisThing)
}

当我尝试编译时,我得到:

error: cannot declare field 'boost::python::objects::value_holder<SomeVirtualClassWrap>::m_held' to be of abstract type 'SomeVirtualClassWrap'
src/lib/objects/sub_mtrl/rebarmodule.c:412:7: note: because the following virtual functions are pure within 'SomeVirtualClassWrap':
ThisIsAHeader.h:42:27: note: virtual SomeVirtualClass* SomeVirtualClass::clone() const
ThisIsAHeader.h:43:30: note: virtual SomeVirtualClassType SomeVirtualClass::getType() const
ThisIsAHeader.h:44:25: note: virtual std::string SomeVirtualClass::getSomeString() const
In file included from tools/boost-for-sds2-2016/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47:0,
from tools/boost-for-sds2-2016/include/boost/python/object/value_holder.hpp:50,
from tools/boost-for-sds2-2016/include/boost/python/object/class_metadata.hpp:11,
from tools/boost-for-sds2-2016/include/boost/python/class.hpp:23,
from tools/boost-for-sds2-2016/include/boost/python.hpp:18

我需要能够从 Python 调用 A.GetThisThing() 并处理该对象。如果可能的话,我宁愿保留现有代码。

编辑:根据评论,我将 no_init 添加到 python 暴露代码中,并且清除了有关纯方法的错误。现在我剩下:

error: no match for call to '(const boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<SomeVirtualClass*>) (SomeVirtualClass*)

编辑:从 Python 暴露中删除 *clone 方法清除了该错误。事实证明我根本不需要那个。只是处理转换错误。

最佳答案

根据 Luka Rahne 的评论,添加 no_init 修复了编译错误:

bp::class_ <SomeVirtualClassWrap, boost::noncopyable>("SomeVirtualClass", no_init)
.def("GetSomeString", bp::pure_virtual(&SomeVirtualClass::getSomeString))
.def("Clone", bp::pure_virtual(&SomeVirtualClass::clone))
.def("GetType", bp::pure_virtual(&SomeVirtualClass::getType));

特别感谢 n.m.以及。

关于c++ - Boost python 和虚拟类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40508667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com