【问题标题】:How to expose derived class function in boost python如何在boost python中公开派生类函数
【发布时间】:2013-12-04 13:37:29
【问题描述】:

我想在派生类(.h 或 .cpp 文件)中实现暴露模块。它工作正常,当我在一个 BOOST_PYTHON_MODULE 中的 main 中公开它时,但是当我在抽象类和派生类中以不同方式公开它时,它会出现错误一个或多个多重定义符号错误。示例代码如下。 p>

 **Base.h**

   class Base
     {
       public:
       virtual void Set(const std::vector<std::string>& AllParameters) = 0;
      };

     struct BaseWrap : Base, wrapper<Base>
     {
        void Set(const std::vector<std::string>& AllParameters)
           {
             this->get_override("Set")(AllParameters);

           }

      **Base.cpp**

        BOOST_PYTHON_MODULE(Example)
          {
            class_<Basewrapper , boost::noncopyable> ("Base")
              .def("Set",pure_virtual(&Base::Set))
               ;
          }

       **Derived.h**

        class Derived : public Base
                 {
                  public:
                     int test(int a, int b);

                 };
             BOOST_PYTHON_MODULE(Example)
            {
                 class_<Derived , boost::noncopyable> ("Derived")
                   .def("test",&Derived ::test)
               ;
          }

        **Derived.cpp**


        void Derived:: test(int a , int b)

         { 
           return a+b; 

           }

【问题讨论】:

    标签: c++ abstract-class boost-python derived-class


    【解决方案1】:

    BOOST_PYTHON_MODULE 宏根据提供的参数定义了一个带有标识符的函数。因此,在Base.cppDerived.h 中定义Example 模块在将它们链接到同一个库时违反了one definition rule。要解决此问题,请考虑将导出每个单独类的函数体拆分为它们自己的函数(即export_base()export_derived()),并拥有一个定义BOOST_PYTHON_MODULE 的文件(example.cpp),其主体调用其他导出功能。这个技术可以看here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2011-01-09
      • 2010-12-25
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2012-12-14
      • 2014-09-17
      相关资源
      最近更新 更多