1

This is my first time I ask on Stackoverflow, so please sympathize if I have any inappropriate writing in this asking!

I'm using in Cython to bind python code to C++ code. My C++ code has a class with template which has an integer. I cannot write correct cython code although I have searched for many posts.

My C++ class in .h file:

namespace abc {
    class Status {
        // declare attributes and methods here
    };

    class Info {
        // declare attributes and methods here
    };

    template <typename T, int n>
    class Using
    {
      public:
        Using() {}
        ~Using() {}
        void display(T a, T b)
        {
            cout << a.s << b.s << endl;    // T will be other class which has `int s` attribute
        }
    };
}  // end of namespace

I tried writing cython code as below but unsuccessfully:

The .pxd file:

cdef extern from "dev.h" namespace "abc":
    cdef cppclass Status:
        // declare attributes and methods of Status class here

cdef extern from "dev.h" namespace "abc":
    cdef cppclass Info:
        // declare attributes and methods of Info class here

cdef extern from "dev.h" namespace "abc":
    cdef cppclass Using[T, int]:
        Using() except +
        void display(T a, T b) except +

The .pyx file:

cimport dev as cpp

cdef class Status:
    ... cdef and def for stuffs of Status class ...

cdef class Info:
    ... cdef and def for stuffs of Info class ...

cdef class Using:
    cdef cpp.Using<cpp.Status, 3> *c_using

    def __cinit__(self):
        self.c_using = new cpp.Using<cpp.Status, 3>()

    def __dealloc__(self):
        del self.c_using

    cdef display(self, cpp.Status obja, cpp.Status objb):
        self.c_using.display(obja, objb)

I compile the cython code with python3 setup.py build_ext --inplace, then it took 2 errors as below:

    cdef cpp.Using<cpp.Status, 3> *c_using
                 ^
Error: Empty declarator

and

    cdef cpp.Using<cpp.Status, 3> *c_using
                 ^
Error: Syntax error in C variable declaration

What could I write in pxd and pyx files for this situation?

Thank so much for your help!

Cao Nguyen
  • 19
  • 2
  • What you're trying to do isn't really supported. [This answer](https://stackoverflow.com/questions/41190519/interfacing-c11-array-with-cython/41200186#41200186) contains my best known work around – DavidW Mar 18 '20 at 07:40
  • your other problem is the typo - it should be `cpp.Using[cpp.Status, ...]`, i.e. `[` and not `>`. – ead Mar 18 '20 at 07:41

0 Answers0