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?