Boost Python:与C++;传回c+时的签名+;导出对象 我尝试使用Python扩展我的C++代码,然后使用BooSt.python从C++导出结构数据,但是在传递参数C++或Python时捕获异常“Error MyRealyyStUSET”。
C++导出代码:Boost Python:与C++;传回c+时的签名+;导出对象 我尝试使用Python扩展我的C++代码,然后使用BooSt.python从C++导出结构数据,但是在传递参数C++或Python时捕获异常“Error MyRealyyStUSET”。,python,c++,boost,export,signature,Python,C++,Boost,Export,Signature,C++导出代码: class TestData { public: int val = 0; }; class World { public: void greet(const TestData& td) { cout << "value: " << td.val << endl; } }; void D
class TestData
{
public:
int val = 0;
};
class World
{
public:
void greet(const TestData& td) {
cout << "value: " << td.val << endl;
}
};
void DoIt(World& w)
{
// do something
}
BOOST_PYTHON_MODULE(PyGameExport)
{
boost::python::class_<World>("World")
.def("greet", &World::greet)
;
boost::python::class_<TestData>("testData", boost::python::init<>())
.def_readwrite("val", &TestData::val);
boost::python::def("doIt", &DoIt);
}
问题1:python函数无法将对象参数传回C++
void test_function2()
{
Py_Initialize();
PyInit_PyGameExport();
using namespace boost::python;
try {
PyObject *pModule = PyImport_ImportModule("Person");
if (!pModule) {
std::cout << PythonException::parse_python_exception() << std::endl;
return;
}
World world;
boost::python::call_method<void>(pModule, "some_func", boost::ref(world));
}
catch (error_already_set) {
PyErr_Print();
}
}
void test_function2()
{
Py_Initialize();
PyInit_PyGameExport();
using namespace boost::python;
try {
PyObject *pModule = PyImport_ImportModule("Person");
if (!pModule) {
std::cout << PythonException::parse_python_exception() << std::endl;
return;
}
World world;
boost::python::call_method<void>(pModule, "some_func", boost::ref(world));
}
catch (error_already_set) {
PyErr_Print();
}
}
void test_function1()
{
Py_Initialize();
PyInit_PyGameExport();
using namespace boost::python;
try {
PyObject *pModule = PyImport_ImportModule("Person");
if (!pModule) {
std::cout << PythonException::parse_python_exception() << std::endl;
return;
}
boost::python::handle<> module(pModule);
boost::python::object main_module(module);
boost::python::object main_namespace = main_module.attr("__dict__");
PyObject* py_obj = NULL;
py_obj = PyRun_String("Person()", Py_eval_input, main_namespace.ptr(), main_namespace.ptr());
if (!py_obj)
return;
World world;
boost::python::call_method<void>(py_obj, "greetReset", boost::ref(world));
}
catch (error_already_set) {
PyErr_Print();
}
}
Traceback (most recent call last):
File "D:\git_dev_home\proj_server\product\Debug\Person.py", line 12, in greetR
eset
instance.greet(d)
Boost.Python.ArgumentError: Python argument types in
World.greet(World, testData)
did not match C++ signature:
greet(class World {lvalue}, class TestData)
Thanks for your answer.