如何解决初始化结构数组 - c++
我正在尝试用 C++ 初始化一个结构数组。
这是我的结构:
typedef ap_fixed<16,1> ap_fixed_data_type;
typedef struct {
ap_fixed_data_type real_part;
ap_fixed_data_type imaginary_part;
} my_data_struct;
这是我的结构数组:
static my_data_struct IFFT_output[1024];
我想使用(如果可能)与标准数组相同的“语法”来初始化我的结构数组,例如:
int my_array[1024] = {0};
这会将我的数组初始化为全 0。
我想要达到的目标是:
static my_data_struct IFFT_output[1024]={{0,0}};
此代码应将每个结构中的每个字段(real_part
和 imaginary_part
)初始化为 0。
使用上面的代码,我收到此错误:
在抛出一个实例后调用终止 '__gnu_cxx::recursive_init_error'
这似乎是由错误的初始化静态变量(如 here)引起的。
我知道我可以用一个简单的 for
循环来初始化我的数据,但我想做一些更“紧凑”的事情。
有没有办法用我上面展示的“语法”来初始化我的结构数组?
解决方法
对我来说这看起来像 C。如果你想使用 C++,你可以:
using ap_fixed_data_type = ap_fixed<16,1>;
struct my_data_struct
{
my_data_struct()
: real_part(/*initialization code here*/),imaginary_part(/*initialization code here*/)
{
// more initialization code here
}
ap_fixed_data_type real_part;
ap_fixed_data_type imaginary_part;
};
std::vector<my_data_struct> vec(1024);
std::array<my_data_struct,1024> array;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。