【发布时间】:2021-01-25 00:25:45
【问题描述】:
我在一个名为 containers.h 的文件中定义了一个名为 List 的模板类:
#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;
}
}
};
}
我正在尝试使用main.pyx 中的代码将此类导入 Cython:
#!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()
这就是我尝试运行这段代码时发生的情况:
>>> 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
为什么会出现此错误,如何解决?
【问题讨论】:
-
可能是来自欺骗stackoverflow.com/a/12574403/5769463 的最相关答案:未定义构造函数
-
这里提供的代码对我来说可以完美编译和运行(这很有意义,因为您已经定义了构造函数)。
标签: python c++ templates cython