初始化列表在 VC10 中不起作用

发布于 2024-09-01 06:48:57 字数 590 浏览 3 评论 0原文

我在 VC++ 2010 中编写了这个程序:

class class1
{
public:
 class1 (initializer_list<int> a){};
 int foo;
 float Bar;
};
void main()
{
 class1 c = {2,3};
 getchar();
}

但是当我编译项目时出现此错误:

错误 1 ​​错误 C2552:“c”: 非聚合无法初始化 带初始值设定项 列出 c:\users\pswin\documents\visual 工作室 2010\projects\test_c++0x\test_c++0x\main.cpp 27

2 IntelliSense:初始化 对象不允许使用“{...}” 类型 “class1”c:\users\pswin\documents\visual 工作室 2010\projects\test_c++0x\test_c++0x\main.cpp 27

问题是什么?

i wrote this program in VC++ 2010:

class class1
{
public:
 class1 (initializer_list<int> a){};
 int foo;
 float Bar;
};
void main()
{
 class1 c = {2,3};
 getchar();
}

but i get this errors when i compile project:

Error 1 error C2552: 'c' :
non-aggregates cannot be initialized
with initializer
list c:\users\pswin\documents\visual
studio
2010\projects\test_c++0x\test_c++0x\main.cpp 27

and

2 IntelliSense: initialization with
'{...}' is not allowed for object of
type
"class1" c:\users\pswin\documents\visual
studio
2010\projects\test_c++0x\test_c++0x\main.cpp 27

what is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2024-09-08 06:48:57

不应支持它 完全

[...] 初始化列表的 C++0x 核心语言功能以及相关的标准库更改未在 VC10 中实现。

该错误消息引用了 C++0x 之前的聚合初始化功能,该功能允许使用大括号初始化某些用户定义类型:

struct pair { int first; char second; };
pair p = { 0, 'c' };

聚合在§8.5.1中定义:

聚合是一个数组或类(第 9 条),没有用户声明的构造函数(12.1),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条),也没有虚拟函数(10.3)。

当聚合被初始化时,初始值设定项可以包含一个初始值设定项子句,该初始值设定项子句由大括号括起来、以逗号分隔的聚合成员的初始值设定项子句列表组成,以递增的下标或成员顺序编写。如果聚合包含子聚合,则此规则递归地应用于子聚合的成员。

It shouldn't be supported at all:

[...] the C++0x Core Language feature of initializer lists and the associated Standard Library changes weren't implemented in VC10.

The error message refers to the pre-C++0x feature of aggregate initialization, which allows the initialization of certain user-defined types by using curly braces:

struct pair { int first; char second; };
pair p = { 0, 'c' };

Aggregates are defined in §8.5.1:

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace- enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文