我必须创建一个程序,用第二个参数替换第一个参数中的所有字母.例如,如果传递的字符串是"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)
string replace(string mystring){
Run Code Online (Sandbox Code Playgroud)
此函数称为replace,将字符串作为参数并返回一个字符串,这由此原型中函数名称前面的类型指示.
如果它希望你返回一个字符串,你不能return void;因为void不是string类型.
所以,你需要return mystring;改为返回一个字符串.
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |