没有 std::string .erase() 的重载函数实例

No instance of overloaded function for std::string .erase()

本文关键字:重载 函数 实例 std string erase 没有      更新时间:2023-10-16

我想实现一个函数来读取返回 std::string 的用户输入。我还希望在返回字符串之前删除回车符,以防万一出现问题(有回车(。

std::string getInput() {
   std::string str = "";
   std::cout << "> ";
   std::getline(std::cin, str);
   if (std::cin.eof()) {
      quitGame();
   }
   str.erase(std::remove(str.begin(), str.end(), 'r'), str.end());
   return str;
}

错误出现在str.erase,它指出no instance of overloaded function但我相信我已经提供了足够的标头并匹配了函数的参数?

有人可以帮我吗?我在这里错过了什么吗?

我已经用 c++ 11、C++ 12 和 c++ 14 编译了你的代码。
它不起作用的唯一原因是缺少算法标头
所以只是

#include <algorithm>
#include <iostream>

它应该可以解决问题 - 这是编译函数所需的唯一两个标头。
另外,您已经声明您使用c ++ 11,因此请确保使用g ++和-std=c ++ 11标志进行编译。