比较两个自动变量 [英] comparison of two auto variables

查看:195
本文介绍了比较两个自动变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

int main()
{
    auto a{1};
    auto b{1};
    if (a==b)
    {
        cout << "equal";
    }
    return 0;
}

为什么上述C ++代码在g ++编译器中返回一个错误, 11标准,而不是打印等于作为输出?

Why does the above C++ code return an error in g++ compiler with c++11 standard, instead of printing "equal" as output?


test.cpp:9:14:错误:无法匹配operator ==(操作数类型为std :: initializer_list和'std :: initializer_list')
if(a == b)
^

test.cpp:9:14: error: no match for ‘operator==’ (operand types are ‘std::initializer_list’ and ‘std::initializer_list’) if (a==b) ^


推荐答案

您认为:

auto a{1};

是指编译器?

如果你认为它应该是整数你错了。编译器是懒惰的确定什么东西,它的initializer_list(正如你的错误所述)和 std :: initializer_list 没有 == 运算符定义(正如您的编译器所声明的)

If you think it's supposed to be integer you are wrong. Compilers are lazy in determining what things are, and it's initializer_list (just as your error stated) And std::initializer_list doesn't have == operator defined (just as your compiler stated)

这篇关于比较两个自动变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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