我是在基于C ++类的python中派生一个类。我想创建一个python类的实例,然后提取一个Card *指针。但我得到一个类型错误说 “没有注册的转换器能够从Fire_Spirit类型的Python对象中提取类型类卡的C ++指针。”
基础C ++类构造函数
Card(string name, string text);
Boost.Python Wrapper
BOOST_PYTHON_MODULE(Card)
{
using namespace boost::python;
class_<Card>("Card", init<string, string>());
}
派生的Python类
from Card import *
class Fire_Spirit(Card):
def __init__(self, name="Fire Spirit", text="A thing"):
self.name = name
self.text = text
super(Fire_Spirit, self).__init__(name, text)
提取代码
using namespace boost::python;
Py_Initialize();
// Get a C++ pointer of the derived python class.
object derived = import("Cards.Fire_Spirit").attr("Fire_Spirit");
object class_instance = derived();
Card* card = extract< Card* >(class_instance);
Py_Finalize();