g++ string remove_if error
g++ string remove_if error
代码:
#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());
}
在VS 2010中编译时,它工作得很好。用g++编译后,它说:
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>)'
将::
添加到isspace
, ispunct
和isdigit
的开头,因为它们有过载,编译器无法决定使用哪个:
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());
添加#include <cctype>
(如果您不是abusing namespace std;
,则说std::isspace
等)。
总是包含你需要的所有头,不要依赖于隐藏的嵌套包含。
您可能还必须消除<locale>
中的重载与另一个重载的歧义。通过添加显式强制转换来实现:
word.erase(std::remove_if(word.begin(), word.end(),
static_cast<int(&)(int)>(std::isspace)),
word.end());
对于我来说,如果我执行以下操作之一,它将使用g++进行编译:
- 移除
using namespace std;
,将string
改为std::string
;或 - 更改
isspace
为::isspace
(等等)。
这两种方法都将导致isspace
(等)从主名称空间中取出,而不是被解释为可能意味着std::isspace
(等)。
问题是std::isspace(int)接受int作为参数,但字符串由char组成。因此,您必须将自己的函数编写为:
bool isspace(char c){返回c == ' ';}
其他两个函数也是如此
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 如何解决"if"块中的"Undeclared identifier error"?
- "[Error] expected unqualified-Id before 'if'"这个代码块是什么意思
- 设置if流,获取'error: expected initializer before ‘.’ token'
- 为什么我会得到一个编译错误,上面写着error:“else”而没有前面的“if”