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

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

问题描述

我试图在Ubuntu(64位)上编译以下代码,将Code :: Blocks 10.05作为IDE:

  #include< iostream> 
using namespace std;
int main(){
char a [2];
cout<< enter ab;
cin>>一个;
if(a =='ab')//第7行
{
cout<< 正确;
}
return 0;
}

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



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

c>定义 char 的数组。 s。 a 是指向数组开头的内存的指针,使用 == a 与'ab',因为它们实际上不是相同的类型, '是整数类型。另外'ab'应该是ab否则你也会有问题。要比较char数组,你想使用strcmp。



可能是说明性的东西是查看 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;
}

这会返回:

  i 
i

'ab'实际上是作为int来计算。



如果你用std做同样的事情: :string然后你将处理一个类,std :: string具有运算符== 重载,并将进行比较检查时调用这种方式。



如果你想用一个惯用的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;
}


I am trying to compile the following code on Ubuntu (64-bit), with Code::Blocks 10.05 as 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]".

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

解决方案

char a[2] defines an array of char's. a is a pointer to the memory at the beginning of the array and using == won't actually compare the contents of a with '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.

Something that might be illustrative is looking at the typeid of '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.

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.

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

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

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

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