我有一个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>())
;
}
出了什么问题,为什么?
答案 0 :(得分:2)
我正在传递对象的包装版本而不是基础。我应该有:
class_<BehaviourDomainWrap, boost::noncopyable>("beh")
.def("subject_count", pure_virtual(& BehaviourDomain::subjectCount))
;
class_<MappedBehaviourDomain, bases<BehaviourDomain> >
("mapped_beh", init<std::string>())
;