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.