error: jump to label ‘xxxxx’ [-fpermissive]

C++中使用goto语句可以跳到指定的函数末端,在使用g++编译时,要注意在goto语句出现之后是不允许出现新申明的变量,所以需要申明变量需要放在所有goto语句之前。(VisutalStudio编译无此问题)。




#include <iostream>


void Test(int m)
{
int i = m;
if (i > 10) goto res;


int j = i;


res:
std::cout<<"m > 10"<<std::endl;


}


int _tmain()
{
Test(4);
return 0;
}
此时使用g++编译报错:

root@ubuntu:/home/Temp# g++ -c temp.cpp
temp.cpp: In function ‘void Test(int)’:
temp.cpp:12:1: error: jump to label ‘res’ [-fpermissive]
 res:
 ^
temp.cpp:7:19: error:   from here [-fpermissive]
  if (i > 10) goto res;
                   ^
temp.cpp:10:6: error:   crosses initialization of ‘int j’
  int j = i;


将Test方法中代码做如下修改即可:


void Test(int m)
{

int i = m;

int j;

if (i > 10) goto res;


 j = i;


res:
std::cout<<"m > 10"<<std::endl;


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值