2012-07-27 43 views
0

我想创建一个python使用Boost Python的升压Python绑定一类

#include <boost/python.hpp> 
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> 
#include <vector> 

using namespace boost::python; 

struct World 
{ 
    void set(std::string msg) { this->msg = msg; } 

    std::string greet() { return msg; } 

    MyList2 getList() { 
     MyList v1(5, 1), v2(10, 2); 
     MyList2 v; 
     v.push_back(v1); 
     v.push_back(v2); 
     std::cout<<"In C++: "<<v.size()<<std::endl; 
     return v; 
    } 

    std::string msg; 
}; 


BOOST_PYTHON_MODULE(test_ext) 
{ 
    class_< std::vector<World> >("MyList") 
     .def(vector_indexing_suite< std::vector<World> >()); 

    class_<World>("World") 
     .def("greet", &World::greet) 
     .def("set", &World::set) 
     .def("list", &World::getList) 
    ; 
} 

一个C++类结合的载体,但我得到的编译错误与向量索引套房试图把一个类的载体时, 。

no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = World*, _Container = std::vector<World>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = World&]() == __val’ 
+0

定义'MyList'和'MyList2' – 2016-03-10 08:06:28

回答

2

Python列表比C++向量有更多的功能。 vector_indexing_suite定义了contains方法,因此您的容器类必须定义operator==

+0

对不起,我忘了怎么办代码标记。现在修复。 – 2012-07-27 20:54:37

+0

我从85英里每小时的汽车上在平板电脑上输入这个答案。不会再尝试:) – 2012-07-27 21:08:49