error: initialization with quot;{...}quot; expected for aggregate object - c++(错误:用“{...}初始化;预期聚合对象 - 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 中存在的数据时
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
pac.datafoo = foo.test2[4096];
unsigned char data[4096] = pac.datafoo;
这是我得到的错误:
error: initialization with "{...}" expected for aggregate object
我做错了什么?
推荐答案
需要学习数组的初始化方法.它不是简单地分配为单个变量.
You need to learn the array initialization method. It's NOT simply assigned as the single variable.
一些例子:
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];
}
这篇关于错误:用“{...}"初始化;预期聚合对象 - C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:用“{...}"初始化;预期聚合对象 - C++
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
