gpt4 book ai didi

c++ - 使用 Boost::Python 包装纯虚函数

转载 作者:行者123 更新时间:2023-11-28 06:20:05 34 4
gpt4 key购买 nike

我现在正在学习使用 Boost::Python 将 c++ 类暴露给 Python,并且我编写了以下代码。

编译代码就可以了。但是当我从 Python 端导入代码时,它显示以下错误:

追溯(最近的调用最后): 文件“test4.py”,第 1 行,在

import shape;

ImportError:/home/ruofan/Boost/Class/shape.so: undefined symbol: _ZTI7Polygon

我该如何解决这个问题?

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

using namespace boost::python;
using namespace std;

class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual int area (void) =0;
void printarea()
{ cout << this->area() << '\n'; }
virtual ~Polygon();
};

class Rectangle: public Polygon {
public:
Rectangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height; }
virtual ~Rectangle();
};

class Triangle: public Polygon {
public:
Triangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height/2; }
virtual ~Triangle();
};

struct BaseWrap : Polygon, wrapper<Polygon> {
BaseWrap() : Polygon(0,0){}
int area(){
return this->get_override("area")();
}
};


BOOST_PYTHON_MODULE(shape){
class_<BaseWrap, boost::noncopyable>("Polygon")
.def("area", pure_virtual(&Polygon::area));
}

最佳答案

问题是非纯虚函数被声明但从未被定义。这导致库对类的类型信息有 undefined reference :

$ c++filt c++filt _ZTI7Polygon
typeinfo for Polygon

在这种特殊情况下,从未定义析构函数:

class Polygon {
...
public:
...
virtual ~Polygon(); // Declared, but never defined.
};

要解决这个问题,请定义 Polygyon 析构函数:

class Polygon {
...
public:
...
virtual ~Polygon() = default;
};

RectangleTriangle 类型也存在同样的问题。虽然,可以省略声明它们的析构函数并允许编译器隐式生成它们。


包装纯虚函数的 Boost.Python 代码看起来不错。这是一个基于原始代码的完整示例 demonstrates它的用法:

#include <boost/python.hpp>

// Mockup abstract type.
class polygon
{
protected:
int width, height;
public:
polygon(int a, int b): width(a), height(b) {}
virtual int area () = 0;
virtual ~polygon() = default;
};

// Wrapper to allow calling Python overrides.
struct polygon_wrap
: polygon, boost::python::wrapper<polygon>
{
polygon_wrap(): polygon(0, 0) {}
int area() { return this->get_override("area")(); }
};

BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;

// Expose models.
python::class_<polygon_wrap, boost::noncopyable>(
"Polygon", python::init<>())
.def("area", python::pure_virtual(&polygon::area))
;
}

交互使用:

>>> import example
>>> class BadPolygon(example.Polygon):
... pass
...
>>> class Square(example.Polygon):
... def __init__(self, length):
... example.Polygon.__init__(self)
... self.length = length
... def area(self):
... return self.length**2
...
>>> try:
... polygon = BadPolygon()
... assert(isinstance(polygon, example.Polygon))
... got_runtime_error = False
... polygon.area()
... except RuntimeError:
... got_runtime_error = True
... finally:
... assert(got_runtime_error)
...
>>> polygon = Square(6)
>>> assert(isinstance(polygon, example.Polygon))
>>> assert(36 == polygon.area())

关于c++ - 使用 Boost::Python 包装纯虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464625/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com