C++11的初始化列表的一个问题,编译错误

msister 2012-05-05 10:21:03

#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

究竟错在哪里?
...全文
845 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsongcui 2012-05-05
  • 打赏
  • 举报
回复
24: struct c2: c1 “{”

31: }“;”

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧