首页 > 解决方案 > 用嵌套类型包装 C++ 类

问题描述

官方文档显示了一个Rectangle.h仅包含原始类型的示例。

如果如何包装这个类

如果我用 Cython 编译它,我会收到以下Unknown type错误Blabla

问:如何告诉 Cython 在哪里可以找到BlaBla?我是否也需要编写自定义包装器BlaBla

一个例子:

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Blabla.h"   // This is the addition from the official example

namespace shapes {
    class Rectangle {
        public:
            int x0, y0, x1, y1;
            Rectangle();
            Rectangle(int x0, int y0, int x1, int y1);
            ~Rectangle();
            int getArea();
            void getSize(int* width, int* height);
            void move(int dx, int dy);
            // New method:
            BlaBla getBla(int x, BlaBla* ptr);
    };
}

#endif

有陪伴.pxd

cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle() except +
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        int getArea()
        void getSize(int* width, int* height)
        void move(int, int)
        // New:
        BlaBla getBla(int x, BlaBla* ptr);

Cython 会抱怨自己BlaBla是一个无法识别的类型

标签: cython

解决方案


推荐阅读