通过引用模板方法传递向量

时间:2013-04-18 21:53:33

标签: c++ python cython

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

我正在使用Cython 0.18& Python 2.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);
}

Cython方

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 []

1 个答案:

答案 0 :(得分:1)

我不确定这实际上是否回答了这个问题,但我还没有发表评论以澄清问题。

当我尝试编译时,我收到了一个错误:

error: no matching function for call to ‘VectByRef::GetVector(std::vector<int>)’

通过将Cython端的调用更改为

来解决错误
.GetVector(<vector[cython.int]&> resultVectInt)

完全删除类型资格也有效:

.GetVector(resultVectInt)

这两个也给出了期望的结果:[1, 2, 3]

这也是使用Python 2.7,同时使用Cython 0.17和0.19。所以我猜其他东西正在发生,或者有0.18的特定错误。

相关问题