Hello I am learning templates and at the moment I have created a templated QuickSort function. It's rather short and self explanatory. However during the call to the QuickSort Driver function I get a No matching call error. I have detailed the error message in full at the bottom of the post. Thank you.

QuickSort.h

#ifndef QuickSort_H
#define QuickSort_H
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

//QUICKSORT DRIVER
template <class Comparable>
void quickSort(vector<Comparable> & a)
{
    quickSort( a, 0, a.size( )-1 );  <---ERROR COMES FROM THIS CALL I'VE DETERMINED
}

//RECURSIVE QUICKSORT FUNCTION
template <class Comparable>
void quickSort(vector<Comparable> & arr, int left, int right) {

    int swaps=0;      
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];

    for(int i=left; i<right+1; i++)
    {
            cout << arr[i] << endl;
    }

    /* partition */
    while (i <= j) 
    {
        while (arr[i] < pivot)  //INCREASES I UNTIL NUMBER GREATER THAN PIVOT
        {
            i++;
        }
        while (arr[j] > pivot)  //DECREASES J UNTIL NUMBER LESS THAN PIVOT
        {
            j--;
        }
        if (i <= j) 
        {
            //SWAPS INDEXES  
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            if(i!=j)
            {
                swaps++;
            }
            i++;
            j--;
        }
    }
    if(swaps==0)
    {
        return;
    }
    /* recursion */
    if (left < j)
        quickSort(arr, left, j);
    if (i < right)
        quickSort(arr, i, right);
}



template <class Comparable>
void checkSort( const vector<Comparable> & a )
{
    for( int i = 0; i < a.size( ); i++ )
    if( a[ i ] != i )
    {
        cout << "Error at " << i << "   " << a[i] << endl;
    }        
    else
    {
        cout << "Success at " << i << "   " << a[i] << endl;
    }
    cout << "Finished checksort" << endl;
}

#endif

Next is my Main TestQuickSort.cpp

#include "QuickSort.h"



int main(){

    const int NUM_ITEMS = 10;

    vector<int> a( NUM_ITEMS );
    for( int i = 0; i < a.size( ); i++ )
    {
        a[ i ] = (a.size()-1)-i;  
    }   

    for( int i =0; i<a.size( ); i++ )
    {
        cout << a[i] << endl;
    }


    quickSort( a );   <-------THIS IS THE CALL THAT GIVES ME THE ERROR
    checkSort( a );       



system("PAUSE");
return 0;
}

I have marked the lines which I determined is the problem. In the main when I call quickSort(a); I get a no matching function for call to quickSort(std::vector<int, std;;allocator<int> > &, int, unsigned int). If i call the function as quickSort( a, 0, a.size( )-1 ) from the main it compiles with no problems. But when I call the driver function that calls the recursive function I get that error.

I have tried calling it as quickSort<int>(a); after researching some topics on templates and that also does not work. I also attempted to declare quickSort<Comparable>( a, 0, a.size( )-1 ); from the driver function but that also does not work. If anyone has anything else I can try thank you, It's probably something very simple im overlooking but I can't see it for my life apparently.

Edit for clarification: Errors are on line 12 for QuickSort.h and line 21 for TestQuickSort.cpp

EDIT AGAIN: I've also determined this compiles on my Dev C++ ide but the error mainly comes from when I try to use a linux system and make file to compile the program from a terminal I have also added the makefile snippet but I don't think I did anything wrong there

`TestQuickSort.exe: TestQuickSort.cpp QuickSort.h

g++ TestQuickSort.cpp QuickSort.h -o TestQuickSort.exe`

Recommended Answers

All 2 Replies

At line 12 in quicksort.h you attempt to call template <class Comparable> void quickSort(vector<Comparable> & arr, int left, int right) before you have declared it, you define it at line 17.

Oh my god thank you. I thought this was a problem before and I tried a forward declaration but in my haste I must have made and error when doing so and thought it did not work.

Thank you for your help problem has been solved. Simple forward declaration problem =(.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.