A predicate sample?

Can someone whats wrong tell me whats wrong with this code

1
2
3
4
5
6
7
8
9
10
11
12
void Generic_algorithm::Predicates(){
	string word1 = "Isaac", word2 = "newton";
	bool aw = isShort(word1, word2);
	sort(word1.begin(), word1.end(), isShort);

	cout << word1 << endl;
}

bool Generic_algorithm::isShort(const string &s1, const string &s2){
	return s1.size() < s2.size();
	
}


Because i got this error
 
Error	1	error C3867: 'Generic_algorithm::isShort': function call missing argument list; use '&Generic_algorithm::isShort' to create a pointer to member	


Am i missing something?


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.
Last edited on
That doesnt make any sense. Member function is much more strict than non-member?

The book also doesnt denote the & on the calling of the isShort().

But thank you very much for helping
Last edited on
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.
Last edited on
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.
Last edited on
Another error pops out saying

Error 1 error C2664: 'bool (const std::string &,const std::string &)' : cannot convert argument 2 from 'char' to 'const std::string &'

I dont understand, i declare the word as string, and the parameters as reference to string.

Why do i get that error?

I know that string is just an array of char. But i never use any char here. Even the function isShort is using string
The way you pass the first two arguments to std::sort suggests you are trying to sort the chars in word1. Is that not what you want?
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.
@Peter87,

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.
Last edited on
words in the book example is probably a vector of strings (std::vector<std::string>).
Hi, Thanks its all working now.

Thank you for helping
Topic archived. No new replies allowed.