Template qsort

This is a discussion on Template qsort within the C++ Programming forums, part of the General Programming Boards category; Hi guys i'm wondering if someone could help me correct what i'm doing wrong ...my qsort code won't compile..Thanks Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    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.
    Last edited by micha_mondeli; 04-06-2005 at 04:14 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Post the errors



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

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Sorry to hijack the thread, but I still don't understand the qsort algorithm. Anyone care to explain it to me?
    MSVC++ 6.0

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It compiles and links cleanly for me. What compiler are you using?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Visual c++ 6.0

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Actually hold up its working now! Strange..Thanks

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Works fine under Dev-C++ 4.9.9.1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. qsort function + template
    By Cpro in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2007, 02:34 AM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21