[英]error: initialization with “{…}” expected for aggregate object - c++
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?我在做什么错误?
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];
}
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];
} ;
unsigned char * data;
data = pac.datafoo;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.