简体   繁体   中英

ISO C++ forbids comparison between pointer and integer [-fpermissive]

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

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!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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