繁体   English   中英

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

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

我有一些问题,使用Boost.Python lib将抽象类暴露给python。 我正在使用1_53_0 boost和Python 33。

有一个与此问题相关的类似线程。 我如何在Python中实现C ++类,由C ++调用? )我遵循eudoxos响应,他在一个抽象类Base的包装器,它有一个返回字符串类型的方法。

我正在做类似的事情,但我不断收到编译错误:

  • 错误1错误C2668:'std :: basic_string <_Elem,_Traits,_Ax> :: basic_string':对重载函数的模糊调用d:\\ temp \\ example \\ example.h 20
  • 8 IntelliSense:从“boost :: python :: detail :: method_result”到“std :: string”的多个用户定义转换适用:d:\\ temp \\ example \\ example.h 20

以上错误与该行有关:

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

从下面的示例中可以看出:

#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))
    ;
}

任何评论都表示赞赏,提前谢谢!

我找到了解决这个问题的方法,它的工作原理如下:

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

但是,根据boost python文档,这只能用于MSVC6 / 7,这不是我的情况,因为我使用的是VS2010(MSVC 10.0)。

暂无
暂无

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

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