简体   繁体   中英

build error while exposing classes to python using boost.python

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!

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM