警告 C4715:并非所有控制路径都返回值 c++ - 无法通过测试 [英] warning C4715: not all control paths return a value c++ - cant pass a test
问题描述
我遇到此代码的问题:
问题是我不断收到警告 C4715,尽管 .exe 运行正常,并且它为我正在尝试解决的问题提供了正确答案.警告使得无法在应用程序内传递任务.请告诉我为什么我在 if 语句中使用的return"不起作用.
The problem is I'm constantly getting warning C4715 despite the fact .exe is running correctly and it gives correct answer to a problem I'm trying to resolve. The warning makes it impossible to pass the task inside the app. Please give me a clue why 'return' used by me in the if sentences doesn't work.
#include <utility>
#include <iostream>
std::pair<int, int> solve(int a, int b) {
if (a == 0 || b == 0) {
std::pair <int, int> kek(a, b);
return kek;
}
else if (a >= 2 * b) {
a = (a - (2 * b));
solve(a, b);
}
else if (b >= 2 * a) {
b = (b - (2 * a));
solve(a, b);
}
else {
std::pair <int, int> kek(a, b);
return kek;
}
}
int main() {
bool result{ solve(22, 5) == std::make_pair(0,1) };
std::cout << result;
return 0;
}
推荐答案
如果 a == 0 || 你的
不为真,solve
函数不会执行 return
语句b == 0a >= 2 * b
或 b >= 2 * a
之一为真.
Your solve
function won't execute return
statement if a == 0 || b == 0
is not true and either one of a >= 2 * b
or b >= 2 * a
is true.
看起来solve
函数中的两个solve(a, b);
应该是return solve(a, b);
.
It seems that the two solve(a, b);
in the solve
function should be return solve(a, b);
.
这篇关于警告 C4715:并非所有控制路径都返回值 c++ - 无法通过测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!