初始值设定项元素不是常量-如何解决错误? [英] initializer element is not constant - how to solve the errors?

查看:1010
本文介绍了初始值设定项元素不是常量-如何解决错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循此我做到了:

static const struct attribute const *attrs1= {
        &foo_attribute.attr,
        NULL,
};
 static const struct attribute_group const attr_group = {
       .attrs = attrs1,
};

但出现以下错误:

error: initializer element is not constant
error: (near initialization for 'attr_group.attrs')

找到解决方案,但不知道如何解决...

found this solution but didn't understand how to solve it...

该行触发了错误:

.attrs = attrs1,

推荐答案

是的,永远不会将另一个struct对象或另一个变量的内容视为可以在初始化器中用于static对象的常量表达式.

Yes, another struct object or the contents of another variable will never be considered a constant expression that could be used in an initializer for a static object.

但是您的第一次初始化也是伪造的.可能是您的意思

But your first initialization also is bogus. Probably you meant

static const struct attribute * const attrs1= &foo_attribute.attr;

因此您对第二个的初始化将读取类似的内容

So your initialization of the second would read something like

static const struct attribute_group attr_group = {
       .attrs = &foo_attribute.attr,
};

这篇关于初始值设定项元素不是常量-如何解决错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆