|
Boost Users : |
From: Nicholas Yue (yue.nicholas_at_[hidden])
Date: 2024-06-28 05:51:54
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 <boost/python.hpp>
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/
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net