boost python 涉及静态和重载成员函数的代码

boost python code involving both static and overloaded member functions

我正在尝试编译一些涉及静态和重载成员函数的提升 python 代码。有什么提示吗?我只是无法使用函数指针对其进行编译(以前从未做过)但可能还有另一条路要走?

#include<iostream>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>


namespace python = boost::python;


class ORM
{
public:
  static void print() {std::cout << "Fou statique!!!!" << std::endl;}
  static void print(std::string st) {std::cout << st << std::endl;}
  static void print(std::string st1, std::string st2) {std::cout << st1 << std::endl; std::cout << st2 << std::endl;}
};

void (ORM::*print1)() = &ORM::print;
void (ORM::*print2)(std::string st) = &ORM::print;
void (ORM::*print3)(std::string st1, std::string st2) = &ORM::print;


BOOST_PYTHON_MODULE(politopy)
{
  python::class_<ORM>("ORM")
    .def("print", &ORM::print1).staticmethod("print")
    .def("print", &ORM::print2).staticmethod("print")
    .def("print", &ORM::print3).staticmethod("print")
;
}

以下更改对我有用: 您需要 #include <string> 才能使用 std::string。您也不需要将函数指针分配为 ORM 的成员,并且只需在函数指针分配中包含参数类型即可。当您声明 BOOST_PYTHON_MODULE() 时,输入 必须 匹配您的库名称(即 libpolitopy)。此外,始终使用 Py_Initialize() 从 C++ 初始化 Python。最后,您只需在 print.

的最终声明中包含一次 .staticmethod() 标记
#include <iostream>
#include <string>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>

namespace python = boost::python;

class ORM
{
public:
    static void print() {std::cout << "Fou statique!!!!" << std::endl;}
    static void print(std::string st) {std::cout << st << std::endl;}
    static void print(std::string st1, std::string st2) { std::cout << st1 << std::endl; std::cout << st2 << std::endl;}
};

void (*print1)() = &ORM::print;
void (*print2)(std::string) = &ORM::print;
void (*print3)(std::string, std::string) = &ORM::print;


BOOST_PYTHON_MODULE(libpolitopy)
{
    Py_Initialize();

    python::class_<ORM>("ORM")
        .def("print", print1)
        .def("print", print2)
        .def("print", print3).staticmethod("print");
}