我正在玩一些语法,发现一些奇怪的编译器规则,想知道是什么原因
C不会编译这个,但C++会:
switch (argc) {
case 0:
int foo;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
C和C++都会编译这个:
switch (argc) {
case 0:
; int foo;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
C将编译这个而不是C++:
switch (argc) {
case 0:
; int foo = 0;
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
gcc -v是gcc version 4.9.3 (MacPorts gcc49 4.9.3_0)如果它很重要.我意识到解决方案是case 0:用大括号包装内容,但我对编译错误的推理更感兴趣
可能重复:
为什么不能在switch语句中声明变量?
我的代码中有一个奇怪的错误:
char choice=Getchar();
switch(choice)
{
case 's':
cout<<" display tree ";
thetree->displaytree();
break;
case 'i':
cout<<" enter value to insert "<<endl;
cin>>value;
thetree->insert(value);
break;
case 'f' :
cout<< "enter value to find ";
cin>>value;
int found=thetree->find(value);
if(found!=-1)
cout<<" found = "<<value<<endl;
else
cout<< " not found " <<value <<endl;
break;
default:
cout <<" invalid entry "<<endl;;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010编译器说:
1>c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label
1> c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(308) : …Run Code Online (Sandbox Code Playgroud) 我的代码中有这个switch语句:
switch(buttonIndex){
case 0:
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
break;
case 1:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:[imagePicker autorelease] animated:YES];
break;
default:
[self openEmailViewInViewController:self];
}
Run Code Online (Sandbox Code Playgroud)
在案例1中的UIImagePickerController实例化中,我收到一个错误:
error:expected expression before 'UIImagePickerController'
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么.思考?
哦,buttonIndex是一个NSInteger
我刚刚在我维护的一些代码中发现了以下部分:
switch (m) {
case 62: { // opening
// some declarations
// do some stuff
break;
case 63:
// do some other stuff
break;
} // closing
default:
// default stuff
break;
}
Run Code Online (Sandbox Code Playgroud)
块打开是为了声明一些局部变量,但是右括号被错误地放置并且发生在后面case 63.
我几个月都没有注意到这一点,因为它在Visual Studio 2010中编译得很好.我已经尝试过调试它,两种情况都可以正常工作.
怎么会这样 ?这是正确的C语法吗?
考虑一下该文件 sample.es6
switch (1) {
case 1:
const foo = 1;
break;
case 2:
const foo = 2;
break;
}
Run Code Online (Sandbox Code Playgroud)
如果我用Node运行它我得到了
$ node --version
v4.2.11
$ node sample.es6
/tmp/sample.es6:6
const foo = 2;
^
SyntaxError: Identifier 'foo' has already been declared
at Object.<anonymous> (/tmp/sample.es6:1:11)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?节点不应该评估const foo = 2;.
我似乎在switch语句中创建新的局部变量时遇到问题.我认为它是我的类标题中的东西,但是甚至在尝试分配新的NSObject时遇到错误.这是我的语法:
-(NSArray *)charactersFromChapter:(NSInteger)number {
NSObject *noError = [[NSObject alloc] init];
//line above does not cause error
NSArray *characters;
switch (number) {
case 1:
NSObject *obj = [[NSObject alloc] init];
//error happens in line above (Expected expression)
characters = [NSArray arrayWithObject:obj];
break;
case 2:
break;
case 3:
break;
}
return characters;
}
Run Code Online (Sandbox Code Playgroud) 为什么这段代码片段运行正常
void foo(int i)
{
switch(i) {
case 1:
{
X x1;
break;
}
case 2:
X x2;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
而以下给出了编译错误('case'标签跳过'x1'的初始化)?
void foo(int i)
{
switch(i) {
case 1:
X x1;
break;
case 2:
X x2;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道使用大括号引入了一个新的范围,因此在我们打开它的开头大括号之前,不会为x1分配存储空间.但x2仍然在一个案例标签内初始化,没有括号.这不是一个错误吗?
我认为在两个代码片段中都可以有条件地跳过x2的初始化
假设:
switch ( test ) {
// Set some variables, call some functions ?
int x = 1 ;
int y = function(x) ;
//
case 1 :
// Process for test = 1
...
break;
case 5 :
// Process for test = 5
...
break;
default :
// Process for all other cases.
...
}
Run Code Online (Sandbox Code Playgroud)
执行我在第一个之前添加的额外代码是"合法的" case吗?我在例子中从未见过这个.
我想更多地了解" 为什么不能在switch语句中声明变量? "
我读了这篇文章,但我并没有完全理解.您可以在switch内部声明变量,但是要decalre并初始化变量或声明类的对象,它会给出complie time error.
请解释一下......
我在这段代码中遇到了编译错误:
switch(event) {
case kCFStreamEventHasBytesAvailable:
UInt8 buf[BUFSIZE];
CFIndex bytesRead = CFReadStreamRead(stream, buf, BUFSIZE);
if (bytesRead > 0) {
handleBytes(buf, bytesRead);
}
break;
case kCFStreamEventErrorOccurred:
NSLog(@"A Read Stream Error Has Occurred!");
case kCFStreamEventEndEncountered:
NSLog(@"A Read Stream Event End!");
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
该行UInt8 buf[BUFSIZE];导致编译器抱怨"UInt8之前的预期表达式"为什么?
谢谢!
c ×5
c++ ×5
objective-c ×3
cocoa ×1
cocoa-touch ×1
const ×1
ecmascript-6 ×1
ios ×1
iphone ×1
javascript ×1
node.js ×1