简体   繁体   English

警告 C4715:并非所有控制路径都返回值 c++ - 无法通过测试

[英]warning C4715: not all control paths return a value c++ - cant pass a test

I'm having an issue with this code:我对这段代码有疑问:

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.问题是我不断收到警告 C4715,尽管 fact.exe 运行正确,并且它为我试图解决的问题提供了正确的答案。 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.请给我一个线索,为什么我在 if 句子中使用的“return”不起作用。

#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;
    }

Your solve function won't execute return statement if a == 0 || b == 0如果a == 0 || b == 0您的solve function 将不会执行return语句a == 0 || b == 0 is not true and either one of a >= 2 * b or b >= 2 * a is true. a == 0 || b == 0不正确,并且a >= 2 * bb >= 2 * a之一为真。

It seems that the two solve(a, b);看来这两个solve(a, b); in the solve function should be return solve(a, b);solve function 应该是return solve(a, b); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM