的std ::阵列C ++ 11初始化语法错误 [英] std::array c++11 initializer syntax error

查看:123
本文介绍了的std ::阵列C ++ 11初始化语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在std ::阵列
即时得到

 敌不过在'myArr,该= {1,5,2,3,4}''运算符='

错误编译此code时

 的#include<&iostream的GT;
#包括LT&;阵列GT;使用命名空间std;INT主(INT ARGC,字符常量*的argv [])
{
    阵列下;整型,5个myArr,该;
    myArr,该= {1,5,2,3,4};    为(自动I:myArr,该)
    {
        COUT<< I<< ENDL;
    }    返回0;
}

但它编译的时候我做在同一行

 阵列< INT,5个myArr,该= {1,5,2,3,4};

如何在seprate行分配值

我需要在类的构造我怎么能做到这一点赋值?

  MyClass类
{
  我的课()
  {
    myArr,该= {1,2,3,4,5}; ///如何分配它//它给错误
  }
};


解决方案

取而代之的是一对大括号的需要两个。

  myArray的= {{1,2,3,4,5}};

the std::array im getting

no match for ‘operator=’ in ‘myarr = {1, 5, 2, 3, 4}’

error when compiling this code

#include <iostream>
#include <array>

using namespace std;

int main(int argc, char const *argv[])
{
    array<int, 5> myarr;
    myarr = {1,5,2,3,4};

    for(auto i : myarr)
    {
        cout << i << endl;
    }

    return 0;
}

but it compiles when i do it on the same line

array<int, 5> myarr = {1,5,2,3,4};

how to assign values on the seprate line

i need to assign values in the class constructor how can i do it ?

class myclass
{
  myclass()
  {
    myarr = {1,2,3,4,5}; /// how to assign it   // it gives errors
  }
};

解决方案

Instead of the one pair of braces you need two.

myarray = {{1,2,3,4,5}};

这篇关于的std ::阵列C ++ 11初始化语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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