错误:使用“{...}”初始化聚合对象 - C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21924399/
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 23:52:56  来源:igfitidea点击:

error: initialization with "{...}" expected for aggregate object - c++

c++data-structures

提问by pistal

struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo

struct foobar
{
unsigned char data[4096];
}

if i want to access the struct, i say foo.test1, foo.test2[4096], etc.. however, when I wish to return the data present in foo.test2 in the following manner

如果我想访问结构,我会说 foo.test1、foo.test2[4096] 等等。但是,当我希望以下列方式返回 foo.test2 中存在的数据时

pac.datafoo = foo.test2[4096];

unsigned char data[4096] =  pac.datafoo;

this is the error I get:

这是我得到的错误:

error: initialization with "{...}" expected for aggregate object

what is the mistake i'm doing?

我在做什么错误?

回答by Charlie

You need to learn the array initialization method. It's NOT simply assigned as the single variable.

你需要学习数组初始化方法。它不是简单地分配为单个变量。

Some examples:

一些例子:

int arrayone[3] = {0}; // assign all items with 0

int arraytwo[3] = {1, 2, 3 }; // assign each item with 1, 2 and 3

int arraythree[3]; // assign arraythree with arraytwo
for (int i = 0; i < 3; ++i) {
    arraythree[i] = arraytwo[i];
}

回答by Steve

add ";" at the end of the struct.

添加 ”;” 在结构的末尾。

struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo ;

struct foobar
{
unsigned char data[4096];
} ;

回答by SChalice

unsigned char * data;

  data = pac.datafoo;