简单的字符串替换给出错误,尽管参数正确
Simple string replace giving error despite correct arguments
我正在尝试使用以下代码使用此处给出的详细信息替换字符串的一部分:
#include <iostream>
#include <string>
using namespace std ;
int main(){
string ori = "this is a test"; // to replace "is a" with "IS A"
string part = "is a";
int posn = ori.find(part);
int len = part.size();
cout << posn << endl;
ori.replace(posn, len, ori, "IS A");
cout << ori;
}
但是,它给出一个很长的错误,开头为:
rnreplacestr.cpp:11:36: error: no matching function for call to ‘std::__cxx11::basic_string<char>::replace(int&, int&, std::__cxx11::string&, const char [5])’
ori.replace(posn, len, ori, "IS A");
^
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from rnreplacestr.cpp:1:
问题出在哪里,如何解决?感谢您的帮助。
错误消息非常正确 - 没有匹配函数。我想你的意思是使用 std::string::replace 的三参数版本。
改变
ori.replace(posn, len, ori, "IS A");
自
ori.replace(posn, len, "IS A");
字符串替换只有 3 个参数而不是 4 个参数。请参阅文档 http://www.cplusplus.com/reference/string/string/replace/
您的生产线
ori.replace(posn, len, ori, "IS A");
应该是
ori.replace(posn, len, "IS A");
相关文章:
- C++声明模板参数阴影模板参数错误
- 为什么 CRTP 模板C++给出无效参数错误?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 参数错误可能与类型不匹配有关?
- printf 和 strftime 的参数错误无效
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 如何修复"没有重载函数需要 2 个参数"错误C++
- 术语不计算为函数采用 1 个参数错误?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- cudaFreeHost() 无效参数错误
- 精神语法不会编译:函数模板参数错误?
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 使用getDefaultCommConfig使用无效的参数错误
- C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配
- 使用SWIG生成的Python库时,向量分配器参数错误
- 错误C2664:无法转换参数错误
- C 作为参数错误的功能
- 命令行参数错误
- 增强Python参数错误