james kerr
james kerr

Reputation: 65

Error with listing words without vowels

Hello I am getting an error Error'unsigned int std::basic_string<char,std::char_traits<char>,std::allocator<char>>::find(_Elem,unsigned int) const' : cannot convert parameter 2 from 'bool (__cdecl *)(char)' to 'const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &' When trying to compile this code, it's goal is to remove any words which have vowels in them from MyWords list and then print out the words which have no vowels.

Upvotes: 0

Views: 110

Answers (3)

Abhishek Bansal
Abhishek Bansal

Reputation: 12705

std::string::find takes a sub-string as input and returns the position of the 1st character match. http://en.cppreference.com/w/cpp/string/basic_string/find

I don't think it can be applied here directly.

Instead, try:

bool vowelPresent = false;
for ( int i = 0; i < word1.size(); i++ )
  if ( isVowel( word1[i] ) ) {
    vowelPresent = true;
    break;
  }

if ( !vowelPresent ) {
  cout << word1 << endl;
}

Or as Adam suggested, you can use the std::find_if function in the <algorithm> header. using std::find_if with std::string

Upvotes: 2

BoBTFish
BoBTFish

Reputation: 19767

This line is the problem:

if (word1.find(isVowel) != std::string::npos) {

You can't "find" a function pointer inside a string. I suggest using std::string::find_first_of, something like this:

if (word1.find_first_of("aeiouAEIOU") != std::string::npos) {

With your current approach, you were probably thinking of std::find_if which does take a predicate function. You would use it something like:

if (std::find_if(std::begin(word1), std::end(word1), isVowel) != std::end(word1) ) { // ...

Upvotes: 1

Adam
Adam

Reputation: 17369

If you want to use isVowel as your search predicate, then you can use std::find_if: http://www.cplusplus.com/forum/beginner/393/

Upvotes: 0

Related Questions