-2

I'm getting Expected expression errors on the following code:

 (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;
    }
}

The error is found where the "for" code is. I put the (void) code in because I will get an Expression result unused error. Any ideas?

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
user2984757
  • 29
  • 3
  • 8
  • What's the generated error exactly? and the error without the `void` before the `for`? – rullof Dec 26 '13 at 19:28
  • This is not related to Xcode. –  Dec 26 '13 at 19:44
  • Also, learn the basics of C (possibly using a book or a tutorial). You have to be aware of the fundamental elements of the language, we are not here to spoon-feed them to you. –  Dec 26 '13 at 19:45
  • @H2CO3 - Thanks for the advice which didn't help me with my problem. I'm using objective-C on the xcode engine. – user2984757 Dec 26 '13 at 20:04
  • @rullof - The error I get without the void is Unused Entity Issue - Expression result unused. I got the void solution from this page: http://stackoverflow.com/questions/7914990/xcode-4-warning-expression-result-unused-for-nsurlconnection – user2984757 Dec 26 '13 at 20:05
  • in `for(t; ... ; ...){}` t is an expression, not a declaration. Declare it (and initialize it) and you'll be Ok. – wildplasser Dec 26 '13 at 20:17

3 Answers3

1

The (void) before the for loop does not make sense.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • The error I get without the void is Unused Entity Issue - Expression result unused. I got the void solution from this page: http://stackoverflow.com/questions/7914990/xcode-4-warning-expression-result-unused-for-nsurlconnection – user2984757 Dec 26 '13 at 20:05
1

You have to remove the (void) before the for loop because it's not a valid c syntax. You can't solve an error with another error.

You may ask the question : Why puting (void) before the for loop prevented the unused expression error. Well that's because the debugger didn't reach it. and it doesn't know for what is for as he expected a resulted value from it to cast it to void.

When the compiler is generating the error: Unused Entity Issue - Expression result unused. That's means that your program is evaluating an expression without using it.

In your case at the for loop if the t variable is already initialized as you want it, you shouldn't put it at the first part as it will be considired as an unused expression.

for(; t < kPlatformsStartTag + kNumPlatforms; t++) { // keep the first expresion empty
    // ...
}
rullof
  • 7,124
  • 6
  • 27
  • 36
  • I think, user2984757 is not talking about warnings because of unused variables but of unused results of an expression (like e.g. calling a function returning an `int` without using its result) instead. Which, in my opinion, is a useless warning anyway, because in C, almost every expression contains an expression which is evaluated only for its side-effects (`x=y;` has an unused result, for instance, and writing `(void)(x=y)` is stupid), there's nothing generally wrong with ignoring the result of a non-void expression. – mafso Dec 26 '13 at 20:02
  • @mafso Tanks i just missed it since he didn't cleared the problem. – rullof Dec 26 '13 at 21:43
0

You've already got answers about the bogus (void), but not about the unused expression.

for(t; t < kCloudsStartTag + kNumClouds; t++)

The initial expression here, t, has absolutely no effect, and for that reason has no business being present at all. The value of t is read and immediately discarded, and any decent compiler will optimise that by not even bothering to read t. You do not need an expression here. You can remove it, and write

for(; t < kCloudsStartTag + kNumClouds; t++)

although personally, I might be tempted to go with a while loop instead.

Edit: reading your code more closely, your code seems to need to give t an initial value.

for(t = 0; t < kCloudsStartTag + kNumClouds; t++)

Either way, your attempt to suppress the warning without understanding what the warning was telling you wasn't a good idea.