I'm referencing a templated quicksort method in my cpp function like this:
Main.cpp
QuickSort<vector<int>>(testData);
Where testData is:
int arr[] = {0, 5, 3, 4, 2, 1, 4};
vector<int> testData (arr, arr + sizeof(arr) / sizeof(arr[0]));
The declaration of quicksort in the .h file is:
Sorting.h
template <typename T>
void QuickSort(std::vector<T>& Unsorted);
And the function definition is:
Sorting.cpp
template <typename T>
void QuickSort(std::vector<T>& Unsorted)
{
//implementation here
}
Am I losing my mind? I'm just trying to pass a vector of ints by reference. Could someone tell me where I'm going wrong?
templates cannot have separate definition and declaration
also
QuickSort<int>(vec);
in case of functions declaration and definition must be in the saem place, ie:
#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 ) {
}
wont compile, when
#include <vector>
template <typename T>
void qs(std::vector<T>&v ) {}
int main() {
std::vector<int> v;
qs(v);
}
compiles just fine, check in stl how template functions are made. The thing is, that compiler must know entire function before its usage, and he doesnt in your case
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.