我有以下简化代码:
#include <stdio.h>
int main ()
{
printf("Hello ");
goto Cleanup;
Cleanup:
char *str = "World\n";
printf("%s\n", str);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误,因为在标签后声明了一个新变量.如果我将标签后的内容(主要是初始化)放在{}块中,则编译成功.
我想在切换的情况下我理解阻塞的原因,但为什么它应该适用于标签?
此错误来自gcc编译器
考虑以下代码:
#include <iostream>
using namespace std;
int main()
{
int x, y, i;
cin >> x >> y >> i;
switch(i) {
case 1:
// int r = x + y; -- OK
int r = 1; // Failed to Compile
cout << r;
break;
case 2:
r = x - y;
cout << r;
break;
};
}
Run Code Online (Sandbox Code Playgroud)
G ++抱怨.crosses initialization of 'int r'我的问题是:
crosses initialization?x + y通过编译,但后来失败了?crosses initialization?编辑 …
我从下载的示例中获取了这段代码:
bool ChatServer::event(QEvent * event)
{
if(event->type() == QEvent::User)
{
UserEvent * ue = static_cast<UserEvent *>(event);
switch(ue->userType)
{
case CR::ErrorEvent:
case CR::LogEvent:
{
TraceEvent * te = static_cast<TraceEvent *>(ue);
if(te->userType == CR::ErrorEvent)
{
error(te->msg);
}
else
{
log(te->msg);
}
}
break;
default:
return false;
}
}
else
{
return QTcpServer::event(event);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在如果编译程序我得到这个错误:
g++ -c -pipe -O2 -Wall -W -DQT_NO_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/share/qt4/mkspecs/freebsd-g++ -I. -I/usr/local/include/qt4/QtCore -I/usr/local/include/qt4/QtNetwork -I/usr/local/include/qt4 -I. -I/usr/local/include/qt4 -I/usr/local/include -o chatserver.o chatserver.cpp
g++ -c -pipe -O2 …Run Code Online (Sandbox Code Playgroud) 我的计算机代码中存在以下错误,并且不了解如何更正它.请提出任何建议.
错误:错误:跳转到案例标签[-fpermissive] | 错误:越过'int sum'|的初始化 错误:在此范围中未声明'exit'
码:
#include <iostream>
#include <cmath>
using namespace std;
void display_menu();
int get_menu_choice();
void get_two_numbers(int &a, int &b);
int add(int a, int b);
int subtract(int a, int b);
int main()
{
int choice;
do
{
display_menu();
choice = get_menu_choice();
int x, y;
switch (choice)
{
case 1: get_two_numbers(x, y);
int sum = add(x, y);
cout << x << " + " << y << " = " << sum << endl;
break;
case 2: get_two_numbers(x, …Run Code Online (Sandbox Code Playgroud) 在调试崩溃时,我在一些代码中遇到了这个问题:
int func()
{
char *p1 = malloc(...);
if (p1 == NULL)
goto err_exit;
char *p2 = malloc(...);
if (p2 == NULL)
goto err_exit;
...
err_exit:
free(p2);
free(p1);
return -1;
}
Run Code Online (Sandbox Code Playgroud)
第一个malloc失败时会出现问题.因为我们跳过初始化p2,它包含随机数据和调用可能free(p2)会崩溃.
我希望/希望这将与C++中的方式相同,其中编译器不允许goto跳过初始化.
我的问题:是跳过标准允许的初始化还是这是gcc实现c99的错误?
我已经阅读了关于"跳转到案例标签"错误的这个问题,但我仍然有一些问题.我在Ubuntu 12.04上使用g ++ 4.7.
此代码给出错误:
int main() {
int foo = 1;
switch(foo) {
case 1:
int i = 0;
i++;
break;
case 2:
i++;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
错误是
jump-to-case-label.cpp: In function ‘int main()’:
jump-to-case-label.cpp:8:8: error: jump to case label [-fpermissive]
jump-to-case-label.cpp:5:9: error: crosses initialization of ‘int i’
Run Code Online (Sandbox Code Playgroud)
但是,这段代码很好,
int main() {
int foo = 1;
switch(foo) {
case 1:
int i;
i = 0;
i++;
break;
case 2:
i++;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
第二个代码是否比第一个代码更危险?我很困惑为什么g ++允许它.
其次,解决此问题的方法是确定初始化变量的范围.如果初始化变量是一个大对象,并且switch语句在while循环中,那么每次输入和离开范围时都不会调用构造函数和析构函数,从而导致效率降低?或者编译器会优化它吗?