ISO C++ 禁止指针和整数之间的比较 [-fpermissive]| [c++]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9280921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:35:56  来源:igfitidea点击:

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

c++

提问by Thunfische

I am trying to compile the following code on Ubuntu (64-bit), with Code::Blocks 10.05 as IDE:

我正在尝试在 Ubuntu(64 位)上编译以下代码,使用 Code::Blocks 10.05 作为 IDE:

#include <iostream>
using namespace std;
int main() {
    char a[2];
    cout << "enter ab ";
    cin >> a;
    if (a == 'ab') // line 7
    {
         cout << "correct";
    }
    return 0;
}

On line 7, my compiler gives me the error "ISO C++ forbids comparison between pointer and integer [-fpermissive]".

在第 7 行,我的编译器给了我错误“ISO C++ 禁止比较指针和整数 [-fpermissive]”。

Why doesn't this work? I know I could use an std::stringto work around the problem, but I want to understand the current problem.

为什么这不起作用?我知道我可以使用 anstd::string来解决这个问题,但我想了解当前的问题。

回答by shuttle87

char a[2]defines an array of char's. ais a pointer to the memory at the beginning of the array and using ==won't actually compare the contents of awith 'ab'because they aren't actually the same types, 'ab'is integer type. Also 'ab'should be "ab"otherwise you'll have problems here too. To compare arrays of char you'd want to use strcmp.

char a[2]定义了一个char's的数组。a是指向数组开头内存的指针, using==实际上不会比较awith的内容,'ab'因为它们实际上不是相同的类型,'ab'是整数类型。也'ab'应该是"ab"否则你在这里也会有问题。要比较您想要使用 strcmp 的字符数组。

Something that might be illustrative is looking at the typeidof 'ab':

可能具有说明性的东西正在查看typeid'ab'

#include <iostream>
#include <typeinfo>
using namespace std;
int main(){
    int some_int =5;
    std::cout << typeid('ab').name() << std::endl;
    std::cout << typeid(some_int).name() << std::endl;
    return 0;
}

on my system this returns:

在我的系统上,这将返回:

i
i

showing that 'ab'is actually evaluated as an int.

显示它'ab'实际上被评估为一个整数。

If you were to do the same thing with a std::string then you would be dealing with a class and std::string has operator ==overloaded and will do a comparison check when called this way.

如果您要对 std::string 做同样的事情,那么您将处理一个类,并且 std::string 已operator ==重载,并且在以这种方式调用时将进行比较检查。

If you wish to compare the input with the string "ab" in an idiomatic c++ way I suggest you do it like so:

如果您希望以惯用的 C++ 方式将输入与字符串“ab”进行比较,我建议您这样做:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string a;
    cout<<"enter ab ";
    cin>>a;
    if(a=="ab"){
         cout<<"correct";
    }
    return 0;
}

回答by dweeves

This one is due to:

这是由于:

if(a=='ab'), here, ais const char*type (ie : array of char)

if(a=='ab'),这里 aconst char*类型(即:字符数组)

'ab'is a constant value,which isn't evaluated as string (because of single quote) but will be evaluated as integer.

'ab'是一个常量值,它不会被评估为字符串(因为单引号),但会被评估为整数。

Since charis a primitive type inherited from C, no operator ==is defined.

由于char是从 C 继承的原始类型,因此没有==定义运算符 。

the good code should be:

好的代码应该是:

if(strcmp(a,"ab")==0), then you'll compare a const char*to another const char*using strcmp.

if(strcmp(a,"ab")==0),然后您将使用 将 aconst char*与另一个const char*进行比较strcmp

回答by Rajpurohit Dhanpal Singh

In Dev-C++, I faced the same problem and I got a solution. I use "instead of "".

在 Dev-C++ 中,我遇到了同样的问题,我得到了解决方案。我使用"代替"".

回答by user13121373

In your code,you wrote char a[2]; which is wrong.You shoud've wrote char* a[2]; Then you can get your proper output.

在你的代码中,你写了 char a[2]; 这是错误的。你应该写 char* a[2]; 然后你可以得到你正确的输出。