G ++ error string remove_if

Here is the code:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string word="";
    getline(cin,word);
    word.erase(remove_if(word.begin(), word.end(), isspace), word.end()); 
    word.erase(remove_if(word.begin(), word.end(), ispunct), word.end()); 
    word.erase(remove_if(word.begin(), word.end(), isdigit), word.end());
}

When compiling in VS 2010, it works fine. Compiled with g ++, it says:

hw4pr3.cpp: In function `int main()':
hw4pr3.cpp:20: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
hw4pr3.cpp:21: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
hw4pr3.cpp:22: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
+5
source share
4 answers

Add ::to the beginning isspace, ispunctand isdigit, since they have overloads that the compiler cannot decide on use:

word.erase(remove_if(word.begin(), word.end(), ::isspace), word.end()); 
word.erase(remove_if(word.begin(), word.end(), ::ispunct), word.end()); 
word.erase(remove_if(word.begin(), word.end(), ::isdigit), word.end());
+14
source

Add #include <cctype>(and say std::isspaceetc. if you are not abusing namespace std;).

Always include all the headers you need and do not rely on hidden nested inclusions.

<locale>. , :

word.erase(std::remove_if(word.begin(), word.end(),
                          static_cast<int(&)(int)>(std::isspace)),
           word.end());
+3

g++, :

  • using namespace std; string std::string;
  • isspace ::isspace ( ..).

, isspace ( ..) std::isspace ( ..).

+2

, std:: isspace (int) int , char. :

bool isspace (char c) {return c == ''; }

.

0

All Articles