3

I have some issue with exposing abstract class to python using Boost.Python lib. I am using 1_53_0 boost and Python 33.

There is a similar thread related to this issue. (How can I implement a C++ class in Python, to be called by C++?) I was following eudoxos response where he makes a wrapper around abstract class Base, which has a method returning string type.

I'm doing something similar but I constantly get a compilation error:

  • Error 1 error C2668: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string' : ambiguous call to overloaded function d:\temp\example\example.h 20
  • 8 IntelliSense: more than one user-defined conversion from "boost::python::detail::method_result" to "std::string" applies: d:\temp\example\example.h 20

The above errors are related to the line:

return this->get_override("getName")();

From my example shown bellow:

#include <string>
#include "boost/python.hpp"

class Person {

public:

    virtual std::string getName() { return "Base"; };

    virtual void setName(std::string name) {};

};

class PersonWrap : public Person, public boost::python::wrapper<Person>
{

public:

    std::string getName()
    {
        return this->get_override("getName")();
    }

    void setName(std::string name)
    {
        this->get_override("setName")();
    }
};

class Student : public Person {

public:

    std::string getName() { return myName; };

    void setName(std::string name) { myName = name; };

    std::string myName;
};

BOOST_PYTHON_MODULE(example)
{
    boost::python::class_<PersonWrap, boost::noncopyable>("Person")
        .def("getName", (&Person::getName))
        .def("setName", (&Person::setName))
    ;

    boost::python::class_<Student, boost::python::bases<Person>>("Student")
        .def("getName", (&Student::getName))
        .def("setName", (&Student::setName))
    ;
}

Any comments are appreciated, thanks in advance!

Community
  • 1
  • 1
  • 1
    If `Person` is pure virtual, why isn't `getName() = 0` and `setName(...) = 0` in their declarations? Are you providing implementations elsewhere? If so, is it correct to wrap `&Person::getName` with `boost::python::pure_virtual`? – legends2k Jun 28 '13 at 12:53
  • Thank you for the comment, I didn't realize the code doesn't match my issue. However, it still doesn't solve my initial problem. – ivanpovazan Jun 28 '13 at 13:17

1 Answers1

2

I have found the solution to this issue and it works like this:

std::string getName()
{
    //return this->get_override("getName")();
    return boost::python::call<std::string>(this->get_override("getName")());
}

However, according to the boost python documentation this has to be used only for MSVC6/7, which is not my case since I am using VS2010 (MSVC 10.0).