std::string::替换错误"no matching function for call"
std::string::replace "no matching function for call" error
我真的不知道如何让这个程序正常工作。目前唯一的问题是replace
。我不能让它工作。我做错了什么?我们只允许使用if
-else
和while
语句。
#include <iostream>
#include <string>
using namespace std;
int main() {
/*i becomes !
a becomes @
m becomes M
B becomes 8
o becomes .
append q*s */
string passWord;
cin >> passWord;
char chari = 'i';
int strLen = passWord.length();
int curPos = 0;
int chariIndex = passWord.find(chari);
while (curPos < strLen) {
if (passWord.find(chari)) {
passWord.replace(chariIndex, 1, '!');
cout << chariIndex << endl;
curPos += 1;
}
}
cout << passWord << endl;
return 0;
}
您没有调用std::string::replace
的任何重载。然而,由于您只替换单个字符,而不是replace
,因此您可以简单地执行
passWord[chariIndex] = '!';
如果你想使用replace
,那么你可以这样称呼它:
passWord.replace(chariIndex, 1, "!");
请注意,第三个参数是string
,而不是char
。
此外,std::string::find
不会像您在代码中所期望的那样返回bool
。您需要将find
和std::string::npos
的结果进行比较:
if (passWord.find(chari) != std::string::npos) {
// found
如果将find
的结果用作bool
,则它将始终计算为true
(除非在第0个索引处找到字符(。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- ESP32-AsyncUDP own handler function for udp.onPacket
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 尝试在矢量上使用擦除功能时"no matching function for call"
- "error: no matching function for call to ‘push_back(char [6])" .如何解决此代码中的错误?
- 使用 std::min "no matching function for call to ‘min(<brace-enclosed initializer list>)’"时出错
- C++字符串.替换生成"No matching function for call"错误
- 如何修复从基类继承两次时"error: no matching function for call to"
- "No matching function for call to ... "在源中使用 CGImage 时C++
- 编译代码时"No matching function for call"。[QTS天线端口打开读取]