Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

recursion - 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. 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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

It seems that the two solve(a, b); in the solve function should be return solve(a, b);.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...