【问题标题】:No instance of overloaded function for std::string .erase()std::string .erase() 没有重载函数的实例
【发布时间】:2019-06-02 12:43:54
【问题描述】:

我想实现一个函数来读取返回 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++ c++11 stdstring


【解决方案1】:

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

#include <algorithm>
#include <iostream>

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

【讨论】:

  • 这可能是问题所在。谢谢,我去看看!
猜你喜欢
  • 2021-10-13
  • 2019-08-11
  • 1970-01-01
  • 2016-03-16
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多