C++11的初始化列表的一个问题,编译错误
- C/C++ code
#include<cstdio>#include<functional>#include<vector>#include<memory>using namespace std;struct s{ int i; s():i(22){printf("ctor\n");} s(const s&ss){i=ss.i;} s& operator=(const s&ss){i=ss.i;return *this;}};struct a{ int i; int j; int k;};struct c1{ int i; int j; int k; c1( std::initializer_list<int> list ){ }};struct c2: c1 c2( std::initializer_list<int> list ) :c1( list ){ }};struct is{ virtual void f()=0;}constexpr int GetFour(){ return 4; }int main(void){ vector<s> buf(3); for(s x:buf){ printf("%d\n",x.i); } const int i=2; char pc[i]; short ps[GetFour()]; a a1={1,2,3}; c2 obj{1,2,3}; return 0;}我在mingw9.0+GCC4.7编译:
////////////////////
D:\>g++ my.cpp
my.cpp:24:2: error: expected '{' before 'c2'
my.cpp:23:1: error: new types may not be defined in a return type
my.cpp:23:1: note: (perhaps a semicolon is missing after the definition of '<typ
e error>')
my.cpp: In function 'int c2(std::initializer_list<int>)':
my.cpp:25:7: error: only constructors take member initializers
my.cpp: At global scope:
my.cpp:27:1: error: expected declaration before '}' token
究竟错在哪里?
[解决办法]
24: struct c2: c1 “{”
31: }“;”