-1

I have a template class called List defined in a file called containers.h:

#include <iostream>
#include <vector>


namespace containers {
    template <class T> class List {
        private:
            std::vector<T> vector;
        public:
            List() {};
            ~List() {};
            void append(T* item) {
                vector.push_back(*item);
            }
            void print_items() {
                for ( T item : vector ) {
                    std::cout << item << std::endl;
                }
            }
    };
}

I'm trying to import this class into Cython using this code in main.pyx:

#!python
# cython: language_level = 3
# distutils: language = c++


cdef extern from "containers.h" namespace "containers":
    cdef cppclass List[T]:
        List() except +
        void append(T *item)
        void print_items()


def test():
    cdef List[int] *l = new List[int]()
    cdef int i
    for i in range(10):
        l.append(&i)
    l.print_items()

And this is what happens when I try to run this code:

>>> import main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/arin/Desktop/Misc/test_cpp/main.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN10containers4ListIiEC1Ev

Why am I getting this error and how do I fix it?

lol cubes
  • 125
  • 7
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ead Oct 10 '20 at 06:56
  • Probably the most relevant answer from dupe https://stackoverflow.com/a/12574403/5769463: constructor isn’t defined – ead Oct 10 '20 at 06:58
  • The code as presented here compiles and runs perfectly for me (which makes sense because you have defined the constructor). – DavidW Oct 10 '20 at 07:51

1 Answers1

0

It was just a silly mistake in compiling: in setup.py I passed "main.cpp" into the cythonize function instead of "main.pyx", and it didn't cause errors while compiling because I had another file called main.cpp.

lol cubes
  • 125
  • 7