问题
The Code
int cycle_length(int i, int j) {
int cycleLength = 0;
for (int k = i; k <= j; k++) {
cout << algorithm(k) << endl;
if (algorithm(k) > cycle_length) {
cycleLength = algorithm(k);
}
}
return cycleLength;
}
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I got this error in this line if ( algorithm(k) > cycle_length)
.
How is that, however, the same code works right in the main()
?? and what is this error mean ???
Added algorithm is a function take an integer input and return an integer.
int algorithm(int number1) {
int counter = 1, number = number1;
do {
if (number % 2 == 0) {
number = number / 2;
counter++;
} else {
number = (3 * number) + 1;
counter++;
}
} while (number != 1);
return counter;
}
回答1:
You are confusing the name of the function with your local variable of nearly the same name:
int cycle_length(int i, int j)
{
int cycleLength
Your function is called cycle_length
, your variable is called cycleLength
- yet you are using cycle_length
further down.
The error message is slightly strange, because the compiler doesn't do "compare variable names with function names to see if there is one that is similar and then suggest that maybe you just typed it wrong" - it simply says "Hmm, you are comparing a function pointer [what you get from the name of a function] with an integer, that's not on!"
来源:https://stackoverflow.com/questions/14589111/iso-c-forbids-comparison-between-pointer-and-integer-fpermissive