I'm trying to sort a Linked List of type T. Here's the parameters for the function that sorts it:

void quicksort(SinglyLL<T> & A, bool (* needSwap)(T &, T &))

needSwap is actually a function pointer that just helps in deciding if swaps are needed - there are two different functions written for the two different data types T can be. needSwap1 handles one datatype, and needSwap2 handles another.

Here's how I'm calling it from the main:

quicksort(list3, needSwap2);

And here's how I'm using it in the quicksort function

// ...
    
    SinglyLL<T> befList;
    SinglyLL<T> piv;
    SinglyLL<T> aftList;

// ...

    typename SinglyLL<T>::iterator itr = piv.begin();
    typename SinglyLL<T>::iterator itr2;
    typename SinglyLL<T>::iterator itr3;

    for (itr2 = A.begin(); itr2 != A.end(); ++itr2)
    {
       if ((*needSwap)(*itr, *itr2))
       // ...
     }

     itr2 = befList.begin();
     itr3 = aftList.begin();
	
     quickSort(befList, needSwap(*itr,*itr2));
     quickSort(aftList, needSwap(*itr3,*itr));

When I try to compile this, it gives me errors about the recursive call. Here's exactly what it says.

listSort.cpp In function `void quicksort(SinglyLL<T>&, bool (*)(T&, T&)) [with T = std::vector<std::string, std::allocator<std::string> >*]':
109 main.cpp instantiated from here
89 listSort.cpp no matching function for call to `quicksort(SinglyLL<std::vector<std::string, std::allocator<std::string> >*>&, bool)'
90 listSort.cpp no matching function for call to `quicksort(SinglyLL<std::vector<std::string, std::allocator<std::string> >*>&, bool)'

That line in the main mentioned is how I showed it being called earlier.

I've also tried variations like

(needSwap)(*itr,*itr2)
(*needSwap)(*itr,*itr2)
(needSwap)(itr,itr2)

But I can't figure out exactly how I'm supposed to call it. Here are the parameters of needSwap2.

bool needSwap2(vector<string>* & vec1, vector<string>* & vec2)

What kind of syntax is needed here? I'm totally lost. Thanks!

>quickSort(befList, needSwap(*itr,*itr2));
>quickSort(aftList, needSwap(*itr3,*itr));
You're not supposed to call needSwap here, just pass the pointer as you did in main:

quickSort(befList, needSwap);
quickSort(aftList, needSwap);

>But I can't figure out exactly how I'm supposed to call it.
When you actually need to call needSwap, you can do so in a multitude of ways, but the two most common are:

needSwap(*itr, *itr2);

and

(*needSwap)(*itr, *itr2);

They're functionally identical, so the latter is typically only used to make it clear that a pointer to a function is being used and not a real function.

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.