我在以下代码中遇到了预期的表达式错误:
(void) for(t; t < kPlatformsStartTag + kNumPlatforms; t++) { //error here
CCSprite *platform = (CCSprite*)[batchNode getChildByTag:t];
CGSize platform_size = platform.contentSize;
CGPoint platform_pos = platform.position;
max_x = platform_pos.x - platform_size.width/2 - 10;
min_x = platform_pos.x + platform_size.width/2 + 10;
float min_y = platform_pos.y + (platform_size.height+bird_size.height)/2 - kPlatformTopPadding;
if(bird_pos.x > max_x &&
bird_pos.x < min_x &&
bird_pos.y > platform_pos.y &&
bird_pos.y < min_y) {
[self jump];
}
}
(void) for(t; t < kCloudsStartTag + kNumClouds; t++) { //error here
CCSprite *cloud = (CCSprite*)[batchNode getChildByTag:t];
CGPoint pos = cloud.position;
pos.y -= delta * cloud.scaleY * 0.8f;
if(pos.y < -cloud.contentSize.height/2) {
currentCloudTag = t;
[self resetCloud];
} else {
cloud.position = pos;
}
}
找到“for”代码的错误。我把(void)代码放入,因为我将得到一个表达式结果未使用的错误。有什么想法吗?
答案 0 :(得分:1)
(void)
循环之前的for
没有意义。
答案 1 :(得分:1)
您必须在(void)
循环之前删除for
,因为它不是有效的c语法。您无法通过其他错误解决错误。
您可能会问这样一个问题:为什么在(void)
循环之前放置for
会阻止未使用的表达式错误。那是因为调试器没有达到它。并且它不知道for
是什么,因为他期望从中得到的结果值将其投射到void
。
当编译器生成错误时:Unused Entity Issue - Expression result unused
。这意味着您的程序正在评估表达式而不使用它。
在for
循环的情况下,如果t
变量已根据需要初始化,则不应将其放在第一部分,因为它将被视为未使用的表达式。
for(; t < kPlatformsStartTag + kNumPlatforms; t++) { // keep the first expresion empty
// ...
}
答案 2 :(得分:0)
你已经得到了关于伪造(void)
的答案,但没有关于未使用的表达的答案。
for(t; t < kCloudsStartTag + kNumClouds; t++)
此处的初始表达式t
完全没有效果,因此根本没有任何业务存在。读取t
的值并立即丢弃,任何体面的编译器都会通过甚至不打算阅读t
来优化它。你这里不需要表达式。你可以删除它,然后写
for(; t < kCloudsStartTag + kNumClouds; t++)
虽然就个人而言,我可能会想要使用while
循环。
编辑:更密切地阅读您的代码,您的代码似乎需要为t
提供初始值。
for(t = 0; t < kCloudsStartTag + kNumClouds; t++)
无论哪种方式,你试图在不了解警告告诉你的情况下压制警告并不是一个好主意。