简体   繁体   English

使用boost.python将类暴露给python时构建错误

[英]build error while exposing classes to python using boost.python

I have some issue with exposing abstract class to python using Boost.Python lib. 我有一些问题,使用Boost.Python lib将抽象类暴露给python。 I am using 1_53_0 boost and Python 33. 我正在使用1_53_0 boost和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. 我如何在Python中实现C ++类,由C ++调用? )我遵循eudoxos响应,他在一个抽象类Base的包装器,它有一个返回字符串类型的方法。

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 错误1错误C2668:'std :: basic_string <_Elem,_Traits,_Ax> :: basic_string':对重载函数的模糊调用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 8 IntelliSense:从“boost :: python :: detail :: method_result”到“std :: string”的多个用户定义转换适用: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). 但是,根据boost python文档,这只能用于MSVC6 / 7,这不是我的情况,因为我使用的是VS2010(MSVC 10.0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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