Error 1 error C3867: 'Generic_algorithm::isShort': function call missing argument list; use '&Generic_algorithm::isShort' to create a pointer to member
As the error message says you should use & to get a pointer to the function.
sort(word1.begin(), word1.end(), &isShort);
This is needed because the function is a member function. Non-member functions the conversion to function pointer is implicit but I think the only reason it's that way is because that's how it was in C and they don't want to break backward compatibility I guess.
Forget what I just said. Turns out I was wrong. I don't really understand why you get that error message from your code but one problem is that you are trying to sort chars in word1 but isShort compares strings.
Peter87 you were right, that just isn't how you fix it.
You can not pass member functions to STL algorithm functions directly (member functions have a hidden "this" pointer attached to them that complicates things). If you don't understand that yet keep learning C++ and it will make sense.
There are 3 ways to pass a member function to a STL function such as for_each/sort etc so it recognizes it as a stand-alone function.
- Make the comparing function static (the 'this pointer' goes away).
- Use a functor, there are many sites explaining this if you don't recognize the term.
- Use std::bind. http://www.cplusplus.com/reference/functional/bind/
Or of course you can just make the comparison function outside of the class.
In your book if they are calling a function directly with sort it means it isn't a member function in a class.
James2250: You are probably right. I automatically assumed the isShort function was declared as static so when I tested it it worked both with and without & so I concluded I had been wrong.
So & is only needed on non-static member functions... I'll will try to remember that.
I tried doing the sort function without the predicates and it works fine. Adding the isShorter on the third argument causes the problem. No idea why. I didnt declare any char and the code in the book is really the same as mine
The string contains chars, and you are trying to sort them. If you want to pass in a function as third argument you need to pass one that compares two chars because that's what being sorted.
If that is not what you want to do please tell us what it is that you are trying to do.
Sorry, Im still learning c++ up until now. But im going to show you what the c++ primer book is trying to say:
The version of sort that takes a binary predicate uses the given predicate in place of < to compare elements. The predicates that we supply to sort must meet the requirements that we’ll describe). For now, what we need to know is that the operation must define a consistent order for all possible elements in the input sequence. Our isShorter function is an example of a function that meets these requirements, so we can pass isShorter to sort. Doing so will reorder the elements by size:
1 2 3 4 5 6 7 8
// comparison function to be used to sort by word length
bool isShorter(const string &s1, const string &s2) {
return s1.size() < s2.size();
}
// sort on word length, shortest to longest
sort(words.begin(), words.end(), isShorter);
If words contains the same data , this call would reorder words so that all the words of length 3 appear before words of length 4, which in turn are followed by words of length 5, and so on.
The code on my first post is the one I created. Modified a little to run this sample code as it is only a code snippet.
Basically, I want to know the exact output of this code to understand it.