繁体   English   中英

接收错误C2664:传递向量时,无法将参数1从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty,_Ax&gt;&&#39; <int> 模板法

[英]Receiving error C2664: cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty,_Ax> &' when passing vector<int> to templated method

我在我的cpp函数中引用了一个模板化的quicksort方法,如下所示:

Main.cpp的

QuickSort<vector<int>>(testData);

testData在哪里:

int arr[] = {0, 5, 3, 4, 2, 1, 4};
vector<int> testData (arr, arr + sizeof(arr) / sizeof(arr[0]));

.h文件中的快速排序声明是:

Sorting.h

template <typename T>
void QuickSort(std::vector<T>& Unsorted);

而功能定义是:

Sorting.cpp

template <typename T>
void QuickSort(std::vector<T>& Unsorted) 
{
         //implementation here
}

我失去了理智吗? 我只是试图通过引用传递一个int的向量。 有人能告诉我哪里出错了吗?

模板不能有单独的定义和声明

QuickSort<int>(vec);

在函数声明和定义的情况下必须在saem位置,即:

#include <vector>

template <typename T>
void qs(std::vector<T>&v );

int main() {
  std::vector<int> v;
  qs(v);
}


void qs(std::vector<T>&v ) { 
}

不会编译,什么时候

#include <vector>

template <typename T>
void qs(std::vector<T>&v ) {}

int main() {
  std::vector<int> v;
  qs(v);
}

编译得很好,检查stl如何制作模板函数。 问题是,编译器在使用之前必须知道整个函数,并且他不在你的情况下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM