ISO C ++禁止比较指针和整数[-fpermissive] [英] ISO C++ forbids comparison between pointer and integer [-fpermissive]

查看:394
本文介绍了ISO C ++禁止比较指针和整数[-fpermissive]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

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]

我在此行if ( algorithm(k) > cycle_length)中遇到此错误.

I got this error in this line if ( algorithm(k) > cycle_length).

这是怎么回事,但是,相同的代码在main()中也可以正常工作?这个错误是什么意思?

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

您的函数称为cycle_length,您的变量称为cycleLength-但是您将在下面使用cycle_length.

Your function is called cycle_length, your variable is called cycleLength - yet you are using cycle_length further down.

该错误消息有些奇怪,因为编译器不会将变量名与函数名进行比较,以查看是否存在相似的变量名,然后建议您键入错误的代码"-它只是说"Hmm ,您正在将函数指针[从函数名称中获得的结果]与一个整数进行比较,该整数不存在!"

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

这篇关于ISO C ++禁止比较指针和整数[-fpermissive]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆