用括号括起来的初始化器列表初始化结构时出错 [英] Error when initializing a struct with a brace-enclosed initializer list
本文介绍了用括号括起来的初始化器列表初始化结构时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
struct CLICKABLE
{
int x;
int y;
BITMAP* alt;
BITMAP* bitmap;
CLICKABLE()
{
alt=0;
}
};
CLICKABLE input={1,2,0,0};
此代码给我以下错误:
无法从括号括起来的初始化程序列表中进行转换
Could not convert from brace-enclosed initializer list
有人可以解释一下为什么编译器会给我这个错误,以及我该如何解决?我还在学习语言.
Could someone explain me why the compiler is giving me this error, and how I can fix it? I'm still learning the language.
推荐答案
您的类具有构造函数,因此它不是聚合的,这意味着您不能使用聚合的初始化.您可以添加具有正确数量和类型的参数的构造函数:
Your class has a constructor, so it isn't an aggregate, meaning you cannot use aggregate initialization. You can add a constructor taking the right number and type of parameters:
struct CLICKABLE
{
int x;
int y;
BITMAP* alt;
BITMAP* bitmap;
CLICKABLE(int x, int y, BITMAP* alt, BITMAP* bitmap)
: x(x), y(y), alt(alt), bitmap(bitmap) { ... }
CLICKABLE() : x(), y(), alt(), bitmap() {}
};
或者,您可以删除用户声明的构造函数,并使用聚合初始化:
Alternatively, you can remove the user declared constructors, and use aggregate initialization:
CLICKABLE a = {}; // all members are zero-initialized
CLICKABLE b = {1,2,0,0};
这篇关于用括号括起来的初始化器列表初始化结构时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文