如何在Cython中包装引用的c ++类?

时间:2018-01-09 14:01:56

标签: c++ cython

我遇到问题包装以下mctdhBasis类的一个方法:

mctdhBasis.h

class mctdhBasis
{
public:
    //  mctdhBasis();
    mctdhBasis();
    ~mctdhBasis();

void Initialize(string filename, ControlParameters& config);
.
.
.
//mctdhNode is a custom class, which is defined in mctdhNode.h
const mctdhNode& MCTDHNode(size_t i)const;
.
.
.
};

为了初始化这个类,我使用:

ControlParameters.h:

class ControlParameters
{
public:
    ControlParameters();
    ~ControlParameters();

    void Initialize(string configfile, ostream & log);
    .
    .
    .
    };

mctdhNode类在

中定义

mctdhNode.h:

class mctdhNode
: public Node
{
 public:
    mctdhNode();
    mctdhNode(ifstream& file, mctdhNode* up, mctdhNodePosition position_);
    ~mctdhNode();

    void Initialize(ifstream & file, mctdhNode * up_, mctdhNodePosition position_);

    void info(ostream& os=cout)const;
.
.
.
}

我的cython代码如下所示:

mctdh.pyx:

from libcpp.string cimport string

cdef extern from "<iostream>" namespace "std":
    cdef cppclass ostream:
        ostream& write(const char*, int) except +
    ostream cout

cdef extern from "../MCTDH/ControlParameters.h":
    cdef cppclass ControlParameters:
        ControlParameters() except +
        void Initialize(string, ostream&)

cdef extern from "../MCTDH/mctdhBasis.h":
    cdef cppclass mctdhBasis:
        mctdhBasis() except +
        void Initialize(string, ControlParameters&)
        const mctdhNode& MCTDHNode(size_t i)

cdef extern from "../MCTDH/mctdhNode.h":
    cdef cppclass mctdhNode:
        mctdhNode() except +
        void info(ostream& os=cout)

cdef ControlParameters config
cdef mctdhBasis basis
cdef const mctdhNode & node

cdef ControlParameters PyControlParameters(string para):
    cdef ControlParameters config
    config.Initialize(para, cout)
    return config

cdef void PymctdhNode(para, filename, size_t i):
      config.Initialize(para, cout)
      basis.Initialize(filename, config)
      node[0] = basis.MCTDHNode(i)

编译完这些文件后,我收到以下错误:

Error compiling Cython file:
------------------------------------------------------------
...
        mctdhNode() except +
        void info(ostream& os=cout)

cdef ControlParameters config
cdef mctdhBasis basis
cdef const mctdhNode & node
                    ^
------------------------------------------------------------


mctdh.pyx:32:21: C++ references cannot be declared; use a pointer instead

当我将错误的行更改为cdef const mctdhNode * node时,我收到以下错误:

Error compiling Cython file:
------------------------------------------------------------
...
    return config

cdef void PymctdhNode(para, filename, size_t i):
      config.Initialize(para, cout)
      basis.Initialize(filename, config)
      node[0] = basis.MCTDHNode(i)
         ^
------------------------------------------------------------


mctdh.pyx:42:10: Assignment to const dereference

最后我改变了 node[0] = basis.MCTDHNode(i)(&node)[0] = basis.MCTDHNode(i)

但它仍然不起作用:

Error compiling Cython file:
------------------------------------------------------------
...
    return config

cdef void PymctdhNode(para, filename, size_t i):
      config.Initialize(para, cout)
      basis.Initialize(filename, config)
      (&node)[0] = basis.MCTDHNode(i)
                                 ^
------------------------------------------------------------


mctdh.pyx:42:34: Cannot assign type 'const mctdhNode &' to 'const mctdhNode *'

所以基本上我的问题是如何使用'const mctdhNode&amp;'类型声明变量。

1 个答案:

答案 0 :(得分:0)

自动指针取消引用在Cython中不像在C中那样起作用。

ptr[0] = <..>不起作用 - 在cython代码中使用:

val = ptr[0]  # De-reference
val = <..>   # Assign to ptr[0]