Hi
I am trying to add an ability to allow for the calling of methods within
a another method that is running an embedded python but I get an error
saying "TypeError: No Python class registered for C++ class MyClass".
#include
class MyClass
{
public:
MyClass() = default;
void method1();
void method2();
void executePython();
};
#include <iostream>
void MyClass::method1()
{
std::cout << "Method 1 called" << std::endl;
}
void MyClass::method2()
{
std::cout << "Method 2 called" << std::endl;
}
void MyClass::executePython()
{
try
{
Py_Initialize();
// Expose the current instance (self) to the embedded Python script
boost::python::object main_namespace;
main_namespace["self"] = boost::python::ptr(this);
// Execute Python code that calls method1 and method2 on self
boost::python::exec("self.method1()\nself.method2()", main_namespace);
Py_Finalize();
}
catch (boost::python::error_already_set&)
{
PyErr_Print();
}
}
int main()
{
MyClass mc;
mc.executePython();
return 0;
}
--
Nicholas Yue
https://www.linkedin.com/in/nicholasyue/