# Thread: Template qsort

1. ## Template qsort

Hi guys i'm wondering if someone could help me correct what i'm doing wrong ...my qsort code won't compile..Thanks

Code:
```template <typename T>
void qsort(T *a, int l, int r) { // sort a[l..r]
if (l < r) {
int i=l-1, j=r;
const T &v = a[r];
for (;;) { // partition: < v | >= v
while (a[++i] < v);
while (v < a[--j]) if (j <= l) break; // (*)
if (i >= j) break;
swap(a[i], a[j]); // exchange misplaced elems.
}
swap(a[i], a[r]);
qsort(a, l, i-1); // recursively sort part 1
qsort(a, i+1, r); // recursively sort part 2
}
}
template <typename T> void swap(T &a, T &b){
T temp = a; a = b; b = temp;
}
int main(){
int ia[6] = { 1, 3, 5, 2, 8, 0 };
double da[6] = { 1.5, 0.5, 3.4, 5.2 };
qsort(ia, 2, 4); // T=int, sort elements ia[2..4]
qsort(da, 0, 5); // T=double, sort elements da[0..5]
}```
THe code compiles but does not build
errors:

qsorttemplate.obj : error LNK2005: _main already defined in Qsort.obj
Debug/Qsort.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

2. Post the errors

edit: and post the full code. Are those lines of code at the bottom inside your main function, or what?

3. Sorry to hijack the thread, but I still don't understand the qsort algorithm. Anyone care to explain it to me?

4. It compiles and links cleanly for me. What compiler are you using?

5. Visual c++ 6.0

6. Actually hold up its working now! Strange..Thanks

7. Works fine under Dev-C++ 4.9.9.1

Popular pages Recent additions