Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问C++;cython中的模板类_Python_C++_Templates_Cython - Fatal编程技术网

Python 访问C++;cython中的模板类

Python 访问C++;cython中的模板类,python,c++,templates,cython,Python,C++,Templates,Cython,我在名为containers.h的文件中定义了一个名为List的模板类: #包括 #包括 命名空间容器{ 模板类列表{ 私人: std::向量; 公众: 列表(){}; ~List(){}; 无效附加(T*项){ 向量。推回(*项); } 作废打印项目(){ 对于(T项:向量){ std::cout编译时只是一个愚蠢的错误:在setup.py中,我将“main.cpp”传递到cythonize函数中,而不是“main.pyx”,编译时它没有引起错误,因为我有另一个文件名为main.cpp,这回答

我在名为
containers.h的文件中定义了一个名为
List
的模板类:

#包括
#包括
命名空间容器{
模板类列表{
私人:
std::向量;
公众:
列表(){};
~List(){};
无效附加(T*项){
向量。推回(*项);
}
作废打印项目(){
对于(T项:向量){

std::cout编译时只是一个愚蠢的错误:在
setup.py
中,我将
“main.cpp”
传递到
cythonize
函数中,而不是
“main.pyx”
,编译时它没有引起错误,因为我有另一个文件名为
main.cpp

,这回答了你的问题吗?可能dupe最相关的回答是:构造函数没有定义此处提供的代码对我来说编译和运行得非常完美(这是有意义的,因为你已经定义了构造函数)。
#!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