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