swith 引起的: expected expression before 'XXX'的 error

本文详细解释了在Swift中使用switch语句时,在case标签后声明和定义UIImage对象会触发错误的原因,并提供了解决方案。通过在case标签后添加分号或使用大括号括起声明语句,可以避免此错误。文章还引用了一个实际的优快云博客链接作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  今天在写switch的时候遇到个奇怪的问题,

 

case DEFAULT:

UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];

image1 = [image1 convertToScaleWithColor:GRAYCOLOR];

 

我直接在case 标签后面 声明 并定义一个 UIImage 对象,但是它提示  expected expression before UIImage的错误,搞了半天没明白哪里出了问题,最后上网找发现,像 case这类标签语句后面 你 不能立即放置一个声明语句, 它们 “can only precede a statement”(后面必须紧跟一个statement 不知道怎么翻译这个 statement )也就是说 在 UIImage前面随便加个 statement 就没问题,

 

case DEFAULT:

; (加了个分号)

 

UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];

image1 = [image1 convertToScaleWithColor:GRAYCOLOR];

 

或者你也可以将你的声明用大括号 括起来,

 

case DEFAULT:

{

 

UIImage *image1 = [UIImage imageNamed:@"fillcolor_10.png"];

image1 = [image1 convertToScaleWithColor:GRAYCOLOR];

}

 

总之我觉得这东西有点古怪,写下来提醒下自己


原文地址:http://blog.youkuaiyun.com/imstyle1001/article/details/6431560

/home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:18: error: stray ‘\316’ in program if (|��θ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:19: error: stray ‘\224’ in program if (|��θ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:20: error: stray ‘\316’ in program if (|Δ��| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:21: error: stray ‘\270’ in program if (|Δ��| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:23: error: stray ‘\316’ in program else if (|��x|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:24: error: stray ‘\224’ in program else if (|��x|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:37: error: stray ‘\316’ in program else if (|Δx|>0.01 || |��y|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:38: error: stray ‘\224’ in program else if (|Δx|>0.01 || |��y|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:26: error: stray ‘\316’ in program RelativeMove(��x, Δy, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:27: error: stray ‘\224’ in program RelativeMove(��x, Δy, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:31: error: stray ‘\316’ in program RelativeMove(Δx, ��y, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:32: error: stray ‘\224’ in program RelativeMove(Δx, ��y, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:45:32: error: stray ‘\316’ in program RelativeMove(0, 0, ��θ); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:45:33: error: stray ‘\224’ in program RelativeMove(0, 0, ��θ); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:45:34: error: stray ‘\316’ in program RelativeMove(0, 0, Δ��); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:45:35: error: stray ‘\270’ in program RelativeMove(0, 0, Δ��); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp: In function ‘geometry_msgs::Twist GetTargetAheadPose(geometry_msgs::Transform&)’: /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:19:5: error: ‘tf2’ has not been declared tf2::Transform baseToTargetTrans = GetTfPose(); ^~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:22:5: error: ‘tf2’ has not been declared tf2::Transform baseToAheadTrans = baseToTargetTrans * targetToAheadTrans; ^~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:25:5: error: ‘outPose’ was not declared in this scope outPose.linear.x = baseToAheadTrans.getOrigin().x(); ^~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:25:5: note: suggested alternative: ‘boxPose_’ outPose.linear.x = baseToAheadTrans.getOrigin().x(); ^~~~~~~ boxPose_ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:25:24: error: ‘baseToAheadTrans’ was not declared in this scope outPose.linear.x = baseToAheadTrans.getOrigin().x(); ^~~~~~~~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:5: error: ‘mat’ was not declared in this scope mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:5: note: suggested alternative: ‘fmaq’ mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~ fmaq /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:16: error: ‘roll’ was not declared in this scope mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:16: note: suggested alternative: ‘atoll’ mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~~ atoll /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:22: error: ‘pitch’ was not declared in this scope mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:27:22: note: suggested alternative: ‘putc’ mat.getRPY(roll, pitch, outPose.angular.z); // 提取偏航角 ^~~~~ putc /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp: In function ‘bool TrackCb(ar_pose::Track::Request&, ar_pose::Track::Response&)’: /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:33:13: error: ‘step’ was not declared in this scope switch (step) { ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:33:13: note: suggested alternative: ‘strsep’ switch (step) { ^~~~ strsep /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:35:17: error: ‘FindTarget’ was not declared in this scope if (FindTarget() < 0) return error; ^~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:35:42: error:error’ was not declared in this scope if (FindTarget() < 0) return error; ^~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:35:42: note: suggested alternative: ‘perror’ if (FindTarget() < 0) return error; ^~~~~ perror /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:37:26: error: ‘targetPose’ was not declared in this scope RelativeMove(targetPose.linear.x, targetPose.linear.y, targetPose.angular.z); ^~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:37:26: note: suggested alternative: ‘fgetpos’ RelativeMove(targetPose.linear.x, targetPose.linear.y, targetPose.angular.z); ^~~~~~~~~~ fgetpos /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:37:13: error: ‘RelativeMove’ was not declared in this scope RelativeMove(targetPose.linear.x, targetPose.linear.y, targetPose.angular.z); ^~~~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:17: error: expected primary-expression before ‘|’ token if (|Δθ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:22: error: expected primary-expression before ‘|’ token if (|Δθ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:24: error: expected primary-expression before ‘>’ token if (|Δθ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:37: error: expected identifier before ‘case’ if (|Δθ| > 0.05) goto case 4; ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:37: error: expected ‘;’ before ‘case’ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:39:43: error: expected:before ‘;’ token if (|Δθ| > 0.05) goto case 4; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:13: error: ‘else’ without a previous ‘if’ else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:22: error: expected primary-expression before ‘|’ token else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:25: error: ‘x’ was not declared in this scope else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:27: error: expected primary-expression before ‘>’ token else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:36: error: expected primary-expression before ‘|’ token else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:39: error: ‘y’ was not declared in this scope else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:39: note: suggested alternative: ‘yn’ else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ yn /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:41: error: expected primary-expression before ‘>’ token else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:53: error: expected identifier before ‘case’ else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:53: error: expected ‘;’ before ‘case’ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:40:59: error: expected:before ‘;’ token else if (|Δx|>0.01 || |Δy|>0.01) goto case 3; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:41:13: error: ‘else’ without a previous ‘if’ else goto case 5; ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:41:23: error: expected identifier before ‘case’ else goto case 5; ^~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:41:23: error: expected ‘;’ before ‘case’ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:41:29: error: expected:before ‘;’ token else goto case 5; ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:28: error: ‘x’ was not declared in this scope RelativeMove(Δx, Δy, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:33: error: ‘y’ was not declared in this scope RelativeMove(Δx, Δy, 0); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:43:33: note: suggested alternative: ‘yn’ RelativeMove(Δx, Δy, 0); ^ yn /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:45:36: error: expected primary-expression before ‘)’ token RelativeMove(0, 0, Δθ); ^ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:47:13: error: ‘SetFinishedFlag’ was not declared in this scope SetFinishedFlag(true); ^~~~~~~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp: In function ‘int8_t RelativeMove(double, double, double)’: /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:52:5: error: ‘cmd’ was not declared in this scope cmd.request.global_frame = "odom"; // 使用odom坐标系 ^~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:56:5: error: ‘relativeMoveClient_’ was not declared in this scope relativeMoveClient_.call(cmd); // 调用服务 ^~~~~~~~~~~~~~~~~~~ /home/bobac3/ros_workspace/src/oryxbot_cruise_ex/src/oryxbot_cruise_ex.cpp:56:5: note: suggested alternative: ‘RelativeMove’ relativeMoveClient_.call(cmd); // 调用服务 ^~~~~~~~~~~~~~~~~~~
最新发布
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值