通过引用模板方法传递向量 我试图通过引用C++模板方法传递向量,但我所得到的是一个空列表,显然参数是通过值传递的。

通过引用模板方法传递向量 我试图通过引用C++模板方法传递向量,但我所得到的是一个空列表,显然参数是通过值传递的。,c++,python,cython,C++,Python,Cython,我正在使用Cython0.18和Python2.7 有什么想法吗 C++端 class VectByRef { public: VectByRef::VectByRef(); template<typename T> void GetVector(T& var); } template<typename T> void VectByRef::GetVector(T& var) { var.push_back(1); var.pus

我正在使用Cython0.18和Python2.7

有什么想法吗

C++端

class VectByRef
{ 
public:
  VectByRef::VectByRef();

  template<typename T>
  void GetVector(T& var);
}

template<typename T>
void VectByRef::GetVector(T& var)
{
  var.push_back(1);
  var.push_back(2);
  var.push_back(3);
}
cdef extern from "VectByRef.h":
  cdef cppclass VectByRef:
    VectByRef() except
    vector[cython.int] GetVector(vector[cython.int])

def getVector(self):
  cdef vector[cython.int] resultVectInt
  self._vectByRef.GetVector(<vector[cython.int]> resultVectInt)
  print(resultVectInt)  # The result is an empty list []
class-VectByRef
{ 
公众:
VectByRef::VectByRef();
模板
无效向量(T&var);
}
模板
void VectByRef::GetVector(T&var)
{
变量推回(1);
变量推回(2);
变量推回(3);
}
Cython侧

class VectByRef
{ 
public:
  VectByRef::VectByRef();

  template<typename T>
  void GetVector(T& var);
}

template<typename T>
void VectByRef::GetVector(T& var)
{
  var.push_back(1);
  var.push_back(2);
  var.push_back(3);
}
cdef extern from "VectByRef.h":
  cdef cppclass VectByRef:
    VectByRef() except
    vector[cython.int] GetVector(vector[cython.int])

def getVector(self):
  cdef vector[cython.int] resultVectInt
  self._vectByRef.GetVector(<vector[cython.int]> resultVectInt)
  print(resultVectInt)  # The result is an empty list []
“VectByRef.h”中的cdef外部: cdef CPPCClass VectByRef: VectByRef()除了 向量[cython.int]获取向量(向量[cython.int]) def getVector(自): cdef向量[cython.int]结果目标 self.\u vectByRef.GetVector(结果目标) 打印(结果目标)#结果为空列表[]
我不确定这是否真的回答了这个问题,但我还不能发表评论以获得澄清

当我试图编译此文件时,出现了一个错误:

错误:调用“VectByRef::GetVector(std::vector)”时没有匹配的函数

通过将Cython端的调用更改为

.GetVector(resultVectInt)
完全取消型式认证也有效:

.GetVector(resultVectInt)
这两种方法都给出了理想的结果:
[1,2,3]


这也使用了Python2.7,包括Cython0.17和0.19。所以我猜要么是发生了其他事情,要么是0.18有一个特定的bug

使用
GetVector
方法,为什么要将它定义为
T
的方法,而不是
VectByRef
的方法(它被声明为)?我的错,现在它被修复了,谢谢。我还有这个问题。我看到你已经在其他网站上发布了这个。你在别的网站上有什么答案吗?