error: expected unqualified-id before ‘decltype’

今天在做读入一组整数并把它们存入一个vector对象,要求先输出第一个和最后一个元素的和,接着输出第二个和倒数第二个元素的和,以此类推。

下面是我的程序:

#include<iostream>
#include<vector>

using namespace std;

int main(void)
{
	vector<int> ivec;
	int num;
	cout<<"请输入你的整数,按Crtl+d退出:";
	while(cin>>num)
		ivec.push_back(num);
        if(ivec.empty())
           {
             cout<<endl<<"没有数据,直接退出!"<<endl;
             return -1;
           }
       cout<<endl<<"输出结果:";
	for(decltype(ivec.size()) beg = 0,decltype(ivec.size()) end =   
		ivec.size() - 1;beg<=end;beg++,end--)
		cout<<ivec[beg]+ivec[end]<<" ";
	cout<<endl;

    return 0;
}
但是编译的时候却报错了,下面是报错打印:

p3_20.cpp: In function ‘int main()’:
p3_20.cpp:14:36: error: expected unqualified-id before ‘decltype’
  for(decltype(ivec.size()) beg = 0,decltype(ivec.size()) end =   
                                    ^
p3_20.cpp:14:36: error: expected ‘;’ before ‘decltype
p3_20.cpp:15:27: error: expected ‘)’ before ‘;’ token
   ivec.size() - 1;beg<=end;beg++,end--)
                           ^
p3_20.cpp:15:28: error: ‘beg’ was not declared in this scope
   ivec.size() - 1;beg<=end;beg++,end--)
                            ^
p3_20.cpp:15:37: error: no post-decrement operator for type
   ivec.size() - 1;beg<=end;beg++,end--)
                                     ^

因为自己是个菜鸡,想了一会才想到是因为在一条语句(一个;分号)中重复声明同一种类型

for(decltype(ivec.size()) beg = 0,decltype(ivec.size()) end = ivec.size() - 1;beg<=end;beg++,end--)
类似于这样   int i,int j;

去掉后面的decltype就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值