user3431010
user3431010

Reputation: 61

How do I declare an enum in a cpp class in cython

I am trying to wrap a c++ library using cython. The c++ header file say MyFile.h declares a class like this:

class MyClass {

public:
    enum MyEnum{
        TYPE0 = 0,
        TYPE1 = 1,
        TYPE2 = 2,   
    };

    MyClass(MyEnum val=TYPE0){
        // ...
    }
    // ...
}

The pxd file has this:

cdef extern from "<MyFile.h>":

    cdef cppclass MyClass:

        cdef enum MyEnum:
            TYPE0 = 0
            TYPE1 = 1
            TYPE2 = 2

        MyClass(MyEnum val=TYPE0) except +

But cython does not compile it. How do I do this?

Upvotes: 3

Views: 1037

Answers (1)

Czarek Tomczak
Czarek Tomczak

Reputation: 20675

Try using namespace:

cdef extern from "MyFile.h" namespace "MyClass":
    cdef enum MyEnum:
        TYPE0 = 0
        TYPE1 = 1
        TYPE2 = 2

Or maybe this will work as well:

cdef extern from "MyFile.h":
    cdef enum MyEnum "MyClass::MyEnum":
        TYPE0 = 0
        ...

Upvotes: 4

Related Questions