0

I am trying to create a python binding for a C++ class using Boost 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)
    ;
}

But i am getting compilation error with vector indexing suite when trying to bind vector of a class.

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’
1
  • define MyList and MyList2 Commented Mar 10, 2016 at 8:06

2 Answers 2

2

Python lists have quite a bit more functionality than C++ vectors. vector_indexing_suite defines contains method among others, so your container class has to define operator==.

Sign up to request clarification or add additional context in comments.

1 Comment

I was typing this answer on my tablet from a car going 85mph. Not going to attempt that again :)
1

For why we need to define the operators, I think following can be checked out:

why do I need comparison operators in boost python vector indexing suite?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.