替换特定字母的函数不会以void返回类型运行

Mik*_*mus 1 c++ codeblocks

我必须创建一个程序,用第二个参数替换第一个参数中的所有字母.例如,如果传递的字符串是"How now cow"并且函数将所有'o'替换为'e',那么新字符串将是:"Hew new cew."...我在第9行继续收到错误,返回无效部分.

#include <iostream>
using namespace std;

string replace(string mystring){
    replace(mystring.begin(), mystring.end(),  'e',  'o');
    return void;
}
Run Code Online (Sandbox Code Playgroud)

je4*_*e4d 6

你只需要返回修改后的字符串,return mystring;而不是使用return void;


Tam*_*man 5

string replace(string mystring){
Run Code Online (Sandbox Code Playgroud)

此函数称为replace,将字符串作为参数并返回一个字符串,这由此原型中函数名称前面的类型指示.

如果它希望你返回一个字符串,你不能return void;因为void不是string类型.

所以,你需要return mystring;改为返回一个字符串.