【问题标题】:boost python code involving both static and overloaded member functions提升涉及静态和重载成员函数的python代码
【发布时间】:2021-08-31 08:47:08
【问题描述】:

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

#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")
;
}

【问题讨论】:

    标签: python c++ boost static overloading


    【解决方案1】:

    以下更改对我有用: 您需要#include &lt;string&gt; 才能使用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");
    }
    

    【讨论】:

    • 感谢您所做的一切,我测试了它,它就像一个魅力!
    • 很高兴能帮上忙!
    猜你喜欢
    • 2010-10-14
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多