7

I want to declare something like that:

cdef vector[Node*] list2node(list my_list):

But Cython gives me this error:

cdef vector[Node*] list2node(list my_list):
                ^
------------------------------------------------------------

mod.pyx:77:20: Expected an identifier or literal

2 Answers 2

8

You shouldn't need the * -- vector[Node] should generate code for a vector of Node pointers. Using Cython 0.14.1:

cdef class Node: 
    pass
cdef vector[Node] list2node():
    pass
cdef vector[int] test_int():
    pass
cdef vector[int*] test_intp(): 
    pass

Generates the C++ code:

static PyTypeObject *__pyx_ptype_3foo_Node = 0;
static std::vector<struct __pyx_obj_3foo_Node *> __pyx_f_3foo_list2node(void);
static std::vector<int> __pyx_f_3foo_test_int(void);
static std::vector<int *> __pyx_f_3foo_test_intp(void);
1
  • how can Node and Node* be the same? im confused
    – Dan D.
    Dec 20, 2021 at 9:25
3

Taking the answer from this SO answer, what you should do is

ctypedef Node* Node_ptr

and then use Node_ptr across your program.

1
  • i like this ctypedef too, making Node confused with Node* is not interesting
    – Dan D.
    Dec 20, 2021 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.