gpt4 book ai didi

c++ - 控制到达非空函数的末尾(仅在特定的 IDE 上)

转载 作者:行者123 更新时间:2023-11-30 01:10:07 28 4
gpt4 key购买 nike

int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);
if(k==n1)
return count;
if(k>n1)
return --count;
}

这是一个函数,返回小于n的二的最大幂。当我在我的 ubuntu 终端 (g++ 4.8.4) 中运行它时,它工作正常。但是当我在 www.hackerrank.com 上运行它时,它给出了一个错误(control reaches end of non void function)。问题是,我参加了这个网站上的许多比赛,并且多次遇到这个问题。

如果您知道我该如何解决,请告诉我。

最佳答案

您可以像这样使用 else if 语句:

int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);
else if(k==n1)
return count;
else // Eliminate compiler errors (warnings)
return --count;
}

或如@juanchopanza 所述:

int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);

if(k==n1)
return count;

// Eliminate compiler errors (warnings)
return --count;
}

它会做与您的代码相同的事情,但不会让编译器怀疑函数没有返回点。

关于c++ - 控制到达非空函数的末尾(仅在特定的 IDE 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38833462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com