Replace
Hi,
I'm trying to replace the word love with the word hate but I get the error message "No matching member function for call to 'replace'".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
int main ()
{
string const inText1 = "I love you";
string::size_type pos = inText1.find("love");
inText1.replace(pos, "hate");
return 0;
}
|
There are two problems.
1. The replace function modifies the string so you need to call it on a non-const string variable.
2. The second argument to replace (between pos and "hate") should be the number of characters that you want to replace.
The assignment that I have requires me to use string const, how should I solved it then?
You could create a non-const copy of inText1 and then call replace on the copy instead.
Last edited on
Topic archived. No new replies allowed.