0

I get errors where i use the find_if function. It says no matching function. I did find that others have come through this error, but i couldn't quite understand the replies. Please can some one correct this & explain what the mistakes are? Any help would be greatly appreciated. Thanks in advance.

//Another way to split strings

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

using std::endl;
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::istream;

istream& getWords(istream&, vector<string>&);
string& removeDelimeters(string&);
bool space(char);
bool not_space(char);
void display(const vector<string>&);

int main()
{
    vector<string> words;

    getWords(cin,words);
    display(words);

    return 0;
}

void display(const vector<string>& vec)
{
    cout<<endl;
    for(vector<string>::const_iterator iter = vec.begin();iter != vec.end();iter++)
    {
        cout<<*iter<<endl;
    }
}

bool space(char c)
{
    return isspace(c);
}

bool not_space(char c)
{
    return !isspace(c);
}

string& removeDelimeters(string& word)
{
    string delim = ",.`~!@#$%^&*()_+=-{}][:';?><|";

    for(unsigned int i = 0;i<word.size();i++)
    {
        for(unsigned int j = 0;j<delim.size();j++)
        {
            if(word[i] == delim[j])
            {
                word.erase(word.begin()+i);     //removes the value at the given index
                break;
            }
        }
    }

    return word;
}

istream& getWords(istream& in, vector<string>& vec)
{
    typedef string::const_iterator iter;

    string initial;

    cout<<"Enter your initial sentance : ";
    getline(cin,initial);

    initial = removeDelimeters(initial);

    iter i = initial.begin();
    while(i != initial.end())
    {
        //ignore leading blanks
        i = find_if(i,initial.end(),not_space);

        //find the end of the word
        iter j = find_if(j,initial.end(),space);

        //copy the characters in [i,j)
        if(i != initial.end())
        {
            vec.push_back(string(i,j));
        }

        i = j;
    }
}

2 Answers 2

6

Aside from the using std::find_if as mentioned by John Dibling, and a whole host of other issues (look at getWords method and what it's doing with the passed in stream, and return type? etc.)

Your main problem is that you are passing in two different iterator types to find_if, the first iterator is a const_iterator - because you assign to a const_iterator, but the second iterator is non const, i.e. initial.begin() - because initial is not const - and the const/non const iterators are different types, which is why it won't find find_if to match...

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

2 Comments

Ah, I changed the typedef to string::iterator and the find_if errors went away. And I didn't use std:: or using std::find_if;. So I think that supports my argument about ADL.
Hey, ya it made the errors go away. Yes, i think i understand the mistakes. I'm a student trying to climb the ladder of life, so i'm really grateful to the people who explained this problem. Thanks.
6

You need to specify the std namespace:

iter j = std::find_if(j,initial.end(),space);

Or do what you did above, and add a using declaration:

using std::find_if;

11 Comments

@OP, also, note the issue with that particular line, you are passing in j as the starting iterator.. presumably you meant i?
But the iterators are in the std namespace, so wouldn't ADL pick up std::find_if?
@Nim +1. I didn't even really look at the code. Figured there was a 89.7% chance that OP was missing std and stopped looking after I found that
@Fred: No, how could ADL pick up find_if? It found the iterators because iter was typedef-ed to string::const_iterator and there is a using std::string
@John: Well, I just tried it and adding std:: doesn't fix the problem.
|

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.