错误:使用#defined常量的预期表达式 [英] Error: expected expression on line using #defined constant

查看:212
本文介绍了错误:使用#defined常量的预期表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做

  if([resourceCompletionReward objectAtIndex:experienceD]!= 0){

但是Xcode给我一个错误:


预期表达式


我已将 experienceD p>

  #define experienceD 0; 

我做错了什么?

解决方案

分号不应该在那里。

  #define experience D 0 



这也是一个很好的做法。以这样的UPPER_CASE_NOTATION命名常量。



为了完整性,我将补充苹果的建议(从 Cocoa的编码指南


一般来说,不要使用#define预处理器命令来创建常量。对于整数常量,使用枚举,对于浮点常量,使用const限定符


以及


如果常量与其他常量无关,可以使用const创建一个整数常量;否则,使用枚举


所以在你的具体情况下,最好将你的常量定义为

  static const int ExperienceD = 0; 


I'm doing this

if ([resourceCompletionReward objectAtIndex:experienceD] != 0) {

But Xcode is giving me an error:

Expected expression

I've defined experienceD as

#define experienceD 0;

What am I doing wrong?

解决方案

The semicolon shouldn't be there.

#define experienceD 0

will compile just fine.

Also it's a good practice to name constants with an UPPER_CASE_NOTATION like this.

For completeness I will add that Apple suggests (from the Coding Guidelines for Cocoa)

In general, don’t use the #define preprocessor command to create constants. For integer constants, use enumerations, and for floating point constants use the const qualifier

and also

You can use const to create an integer constant if the constant is unrelated to other constants; otherwise, use enumeration

So in your specific case it would be better to define your constant as

static const int ExperienceD = 0;

这篇关于错误:使用#defined常量的预期表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆