【问题标题】:classes not wrappable by boost::pythonboost::python 不能包装的类
【发布时间】:2012-06-16 21:11:00
【问题描述】:

是否有某些类不能被 boost::python 包装?我正在尝试包装一个类,但是在使用它而不是一个虚拟类时遇到了错误。

以下不起作用...

#include <manu/manu.h>

void foo()
{
    // can call non-member Kernel(), which returns Kernel instance
    manu::Kernel* kernel = manu::Kernel(); 
}

BOOST_PYTHON_MODULE(mymodule)
{
    using namespace boost::python;
    class_<manu::Kernel>("Kernel", no_init) // doesn't like this
    ;
}

我可以在函数中使用它,但是将它放在 class_ 模板中会出现以下错误:

manu_python.cpp:9: error: expected constructor, destructor, or type conversion before ‘*’ token
manu_python.cpp: In function ‘void init_module_manu()’:
manu_python.cpp:17: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T, class X1, class X2, class X3> class boost::python::class_’
manu_python.cpp:17: error:   expected a type, got ‘manu::Kernel’

但使用虚拟类确实有效。

class Foo
{
};

BOOST_PYTHON_MODULE(mymodule)
{
    using namespace boost::python;
    class_<Foo>("Kernel", no_init)
    ;
}

完整的类在 manu.h 中定义,但没有在此处完全声明。这个类不能暴露给python吗?

在 manu.h 中,有一个名为 Kernel 的类和一个名为 Kernel() 的非成员函数,它返回一个 Kernel 的实例。这个函数是否会影响 Kernel 类在模板中的使用?如果是这样,有没有办法告诉模板我指的是类而不是函数声明?

【问题讨论】:

  • 在您的示例中,您正在调用 manu::Kernel() 并返回指向 manu::Kernel 的指针。 manu::Kernel 到底是什么?
  • 对不起。那是一个函数,它返回内核的一个实例。该函数恰好与类具有相同的名称。有问题吗?
  • 我不是专家,但我的猜测是编译器将 class_<:kernel> 中的 manu::Kernel 解析为函数类型,而不是类。尝试将您的 manu::Kernel 函数的名称更改为其他名称(可能是 CreateKernel 或其他名称)。

标签: c++ templates boost boost-python


【解决方案1】:

我遇到了同样的问题并找到了这个网站: http://qiita.com/nishio/items/daff0e9be30f4b4d6fca

几个可读的位为我解决了这个问题:

#include <boost/python/class.hpp>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2018-05-20
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多