3

我正在尝试使用 MSVC2013 编译以下内容:

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{false, false, false};
}

编译器给我以下错误:SomeClass::SomeStruct::SomeStruct: no overloaded function takes 3 arguments.

如果我将代码更改为:

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{{false, false, false}};
}

该程序编译并运行良好。这是编译器的问题,还是我不懂语法?从我一直在阅读的内容来看,第一个版本应该可以编译。

4

2 回答 2

1

如果根据需要放置分号

class SomeClass
{
    struct SomeStruct { bool a,b,c; };
    SomeStruct ss{false, false, false};
};

那么它似乎是MS VC++ 2013编译器的一个错误。至少代码在 www.ideone.com 上编译成功。

结构 SomeStruct 是一个聚合,应该使用初始化列表进行初始化。

于 2014-06-04T11:11:07.583 回答
1

Here is the responsible grammar from N3797:

// after a member declaration:
braced-or-equal-initializer-list:
  = initializer-clause
  braced-init-list

braced-init-list:
  { initializer-list ,OPT }
  { }

initializer-list:
  initializer-clause
  initializer-list, initializer-clause

initializer-clause:
  assignment-expression
  braced-init-list

So I'd say the first statement is correct and it is indeed accepted by a recent gcc and clang.

于 2014-06-04T11:07:41.100 回答