1

我有一个 C++ 对象,它是一个虚拟类的子类,我正在尝试用 python 包装。该文件可以编译,我可以将其导入 python,但是当我尝试调用该函数时,出现错误:

In [3]: x
Out[3]: <beh.MappedBehaviourDomain at 0x23e7158>

//So, obviously the constructor is working (or at least thinks it is)

In [4]: x.subject_count()
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
/home/max/verk/btr-email/build/x86_64/bin/ipython in <module>()
----> 1 x.subject_count()

ArgumentError: Python argument types in
    BehaviourDomainWrap.subject_count(MappedBehaviourDomain)
did not match C++ signature:
    subject_count(BehaviourDomainWrap {lvalue})
    subject_count(BehaviourDomainWrap {lvalue})

我很难理解这个错误;这是我第一次使用 boost python,我对 C++ 没有太多经验。以下是相关代码:

// Instantiating class for use in boost python
struct BehaviourDomainWrap : BehaviourDomain, wrapper<BehaviourDomain>
{
    size_t subjectCount() const {
        return this->get_override("subjectCount")();
    // A bunch of other methods removed
}


BOOST_PYTHON_MODULE(beh) {

    class_<BehaviourDomainWrap, boost::noncopyable>("BehaviourDomainWrap")
        .def("subject_count", pure_virtual(& BehaviourDomainWrap::subjectCount))
    ;

    class_<MappedBehaviourDomain, bases<BehaviourDomainWrap> >
        ("MappedBehaviourDomain", init<std::string>())
    ;
}

出了什么问题,为什么?

4

1 回答 1

2

我传递的是对象的包装版本而不是基础。我应该有:

class_<BehaviourDomainWrap, boost::noncopyable>("beh")
    .def("subject_count", pure_virtual(& BehaviourDomain::subjectCount))
;

class_<MappedBehaviourDomain, bases<BehaviourDomain> >
    ("mapped_beh", init<std::string>())
;
于 2012-12-12T17:03:16.250 回答