问题
The problem is basically about to generate a arithmetic expression from a given 'n' numbers , and the expression should be divisible by 101. we can only have +,-,* operators and expression is left associative.
I have tried all the available solution on , that are already mentioned on stack overflow, like closing the expressions with else and many more
bool calci(int a[],int n,int sum,int pos,char operato[],deque<int>&q,deque<char>&qc)
{
if(sum%101==0)
{ //cout<<"sum:"<<sum<<endl;
return true;
}
else if(pos==n)
{return false;}
else
{
for(int k=0;k<3;k++)
{ // cout<<"here for "<<a[pos]<<endl;
sum=addto(sum,a[pos],operato[k]);
qc.push_back(operato[k]);
q.push_back(a[pos]);
bool kyaagesemila=calci(a,n,sum,pos+1,operato,q,qc);
if(kyaagesemila)
{
return true;
}
sum=removefrom(sum,a[pos],operato[k]);
qc.pop_back();
q.pop_back();
}
}
}
i am getting a compilation error
solution.cc:53:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1plus: some warnings being treated as errors```
回答1:
The error message is clear enough. The function has the return type bool
but returns nothing in some execution paths.
Where is there in the last else statement a return statement with a boolean expression?
Or you need to rewrite the last else statement such a way that it would return a boolean expression outside the loop.
For example after the loop you can add statement
return false;
P.S. Use English words as variable identifiers.
回答2:
It's exactly what it says: your code doesn't always return (or, if it does, that's due to logic that the compiler doesn't have the capability or desire to work out).
If control enters the loop in the else
, and no iteration has kyaagesemila == true
, it will simply never reach a return
statement.
Put a return false;
or a return true;
at the end of the else
, according to your desired logic.
来源:https://stackoverflow.com/questions/57221579/error-control-reaches-end-of-non-void-function-werror-return-type