0

So I have a assignment ask me to use randomized quick sort, and I found problems using function pointers.

The sort function is in rqs.cpp:

template <typename Item_Type>
void rqs_with_range(std::vector<Item_Type> &vec, int p, int q,
                int (*cmp)(Item_Type, Item_Type));

Then in my cpp file, I have something like this:

class Table{
   constructor....
   vector< vector<string>* >* holder; // table 
   int compare_str(vector<string>* a, vector<string>* b) {
      return a->at(compare_column) < b->at(compare_column) ? -1 :a->at(compare_column) == b->at(compare_column) ? 0 : 1;
  }

   void rqs{
      rqs_with_range( (*holder) , 1, int(holder->size()-1), &Table::compare_str);
   }
}

The compiler says I have error in function rqs, no matching function. my compare function is member function of Table, would it be the cause of problem?

1
  • 1
    Member functions are not functions, and pointers-to-member-functions aren't function pointers. Search this site, as this has been asked a million times already. Commented Mar 3, 2012 at 18:07

2 Answers 2

1

Yes, that is a problem.

You could either make it a free function or a static member. Otherwise you have a hidden this parameter that affects the function signature.

Sign up to request clarification or add additional context in comments.

Comments

0

If you already are using templates, then please don't use a function pointer. Use a function object instead:

template <typename Item_Type, typename Compare>
void rqs_with_range(std::vector<Item_Type> &vec, int p, int q, Compare comp);

Then the next problem: Make compare_str not a non-static member function, i.e. a function outside the Table class. With the modified rqs_with_range you are also able to solve your problem with std::mem_fun or std::bind but this does not make sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.