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

瘦欲@ 提交于 2021-02-11 17:53:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!