C++ 删除\u如果尝试删除空白时抛出错误

C++ 删除\u如果尝试删除空白时抛出错误,c++,C++,我正在尝试从字符串中删除空格。但我犯了个错误 我的代码做错了哪个参数。。谢谢你的关注 我的主要职能 #include <algorithm> #include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; int main() { string my

我正在尝试从字符串中删除空格。但我犯了个错误

我的代码做错了哪个参数。。谢谢你的关注

我的主要职能

#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;


int main()
{
    string myText;
    myText = readText("file.txt");
    myText.erase(remove_if(myText.begin(), myText.end(), isspace), myText.end());
    cout << myText << endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串myText;
myText=readText(“file.txt”);
删除(如果(myText.begin()、myText.end()、isspace)、myText.end())删除;

您之所以会出现此错误,是因为有两个函数名为
isspace

  • locale
    标题中定义,命名空间标准:

  • 因此,如果要使用第二个函数,有两种方法:

  • 不要使用
    使用命名空间std
    。我更喜欢它
  • 使用
    ::
    调用在全局命名空间中定义的函数


  • 以下是一些详细的解释:

    总之,isspace对编译器来说是模棱两可的。我宁愿命令不使用它

    下面的代码在G++4.7.2中工作

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    bool isSpace(const char& c)
    {
        return !!::isspace(c);
    }
    
    int main()
    {
        string text("aaa bbb ccc");
        text.erase(remove_if(text.begin(), text.end(), isSpace), text.end());
        cout << text << endl;
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    布尔isSpace(常量字符和c)
    {
    return!!::isspace(c);
    }
    int main()
    {
    字符串文本(“aaa bbb ccc”);
    text.erase(如果(text.begin()、text.end()、isSpace)、text.end()删除_);
    
    你能用
    指令/声明显示你的include和任何
    吗?@juanchopanza添加了我的include语句。啊,
    使用命名空间std
    再次罢工。。。
    template<class charT>
    bool std::isspace(charT ch, const locale& loc);
    
    int isspace( int ch );
    
    remove_if(myText.begin(), myText.end(), ::isspace)
    //                                      ^^
    
    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    bool isSpace(const char& c)
    {
        return !!::isspace(c);
    }
    
    int main()
    {
        string text("aaa bbb ccc");
        text.erase(remove_if(text.begin(), text.end(), isSpace), text.end());
        cout << text << endl;
        return 0;
    }