![](/img/trans.png)
[英]c++, “Error: initialization with '{…}' expected for aggregate object”
[英]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];
}
如果我想訪問結構,我會說 foo.test1、foo.test2[4096] 等等。但是,當我希望以下列方式返回 foo.test2 中存在的數據時
pac.datafoo = foo.test2[4096];
unsigned char data[4096] = pac.datafoo;
這是我得到的錯誤:
error: initialization with "{...}" expected for aggregate object
我在做什么錯誤?
你需要學習數組初始化方法。 它不是簡單地分配為單個變量。
一些例子:
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];
}
添加 ”;” 在結構的末尾。
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.