【发布时间】:2023-01-25 00:43:59
【问题描述】:
这段代码有什么问题。我收到一个错误 -
第 16 行:字符 16:错误:从“int”类型的返回值到函数返回类型“std::string”(又名“basic_string”)的转换不可行 返回计数;
class Solution {
public:
string truncateSentence(string s, int k) {
int count=0;
for(char it : s){
if(it == ' '){
if(count<k){
count++;
}
else {
break;
}
}
}
return count;
}
};
【问题讨论】:
-
Count 是一个整数,函数被声明为返回一个字符串。有什么不清楚的错误?