簡體   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