0
我现在正在学习如何使用Boost::Python
将C++类暴露给Python,并且我编写了下面的代码。使用Boost封装纯虚函数:: Python
编译代码是好的。但是,当我从导入的Python端的代码,它显示了以下错误:
回溯(最近通话最后一个): 文件 “test4.py”,1号线,在
import shape;
导入错误:/ 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));
}
太谢谢你了!是的,你是对的,这真的很有帮助! – 2015-04-09 19:19:42