XCode Errors

Started by
10 comments, last by Charabis 17 years, 1 month ago
Okay, trying to compile a program, but XCode is going really screwy . First, the code snippet:

	if (direction)
	{
		switch (rot)
		{
			case 0:
                    {
                        Point2i temp = p1;
						p1 = p2;
						p2 = p3;
						temp.setx (temp.getx() + TILESIZE);
						p3 = temp;
						rot = 3;
						break;
                    }

			case 1:		p3.sety (p3.gety() - TILESIZE);
						rot = 0;
						break;

			case 2:		p2.setx (p2.getx() - TILESIZE);
						rot = 1;
						break;

			case 3:		p1.sety (p1.gety() - TILESIZE);
						rot = 2;
						break;
		}
	}
	else
	{
		switch (rot)
		{
			case 0:		p3.sety (p3.gety() - TILESIZE);
						rot = 1;
						break;

			case 1:		p2.setx (p2.getx() + TILESIZE);
						rot = 2;
						break;

			case 2:		p1.sety (p1.gety() + TILESIZE);
						rot = 3;
						break;

			case 3:		Point2i temp = p3;
						p3 = p2;
						p2 = p1;
						temp.setx (temp.getx() - TILESIZE);
						p1 = temp;
						break;
		}
	}
On the first switch, it chokes on the 'case 0:' line, complaining that
Quote: error: crosses initializtion of 'Point2i temp'
Not only that, but that one error is reported 3 times, along with this error on each of the following case lines within that section of the if.
Quote: error: jump to case label
Oddly, the else portion works fine as is. I did find these could be eliminated simply by declaring the variable BEFORE the switch, although that means it will be created EVERY time it's called instead of in just the one case where it's needed. To add insult to injury, if I change it to the way it will work, it then throws me this error:
Quote: Undefined symbols: _TILESIZE
Which makes no sense, considering:

const extern int TILESIZE;
is in the Tring.h header file it includes, along with the definition

const int TILESIZE = 10;
in main.cpp So what the heck exactly is going on? At this point, I'm at a complete loss. Not to mention it's a heck of a way to start my second day of working with XCode [razz] [Edited by - Charabis on March 8, 2007 4:31:44 PM]
My name is "Todd". Drop the "T" and it explains sooooooo much! :P
Advertisement
Well to fix your first error, without having to move the Point2i outside of the switch statement.

case 0: {    Point2i temp = p1;    p1 = p2;    p2 = p3;    temp.setx (temp.getx() + TILESIZE);    p3 = temp;     rot = 3;    break;}


just add curly braces like so.
Edit your post, and change your "quote" blocks to "source" blocks (or "code" blocks for small chunks of code).
That did indeed solve the first problem, although I'm not completely sure why.

I went back and changed the appropriate quotes to code and source, but it didn't help with either problem [lol]
My name is "Todd". Drop the "T" and it explains sooooooo much! :P
Well, you should also learn how to use whitespace in your code. :)

While you can't type a tab into the web editor, you can paste chunk of code that contains tabs.

These are things that help people (including you) read your code.

Do you understand indentation styles, or is it just a web formatting problem?

Quote:That did indeed solve the first problem, although I'm not completely sure why.


The variable "temp" was visible in every case of your switch statement. This is illegal in C++. The squiggly braces block prevents the problem.

It's a problem with switch statements, by adding the curly braces you force that variable into its own scope within the case, so when it leaves the curly braces that variable is destroyed.
Actually, it looked a lot better when I started, Notayakk. Problem is I pasted the code into the Mac forums trying to find an answer, and it stripped out all my whitespaces. Feeling somewhat lazy, I just copied and pasted from there. Didn't even notice it had stripped all my whitespace out. I'll edit real quick.

EDIT : Although it's still a bit off on the alignment from what I show in my editors. Guess that's what I get for using the tab key to align my expressions [grin]
My name is "Todd". Drop the "T" and it explains sooooooo much! :P
I now have a limited fix for the variable.
const int TILESIZE = 10;


If I pull the 'const', I can get it to pull the variable into the other files. Can constants not be pulled into a file using the 'extern' keyword?
My name is "Todd". Drop the "T" and it explains sooooooo much! :P
const int TILESIZE = 10;
means
static const int TILESIZE = 10;

ie, const variables in C++ are implicitly of internal linkage.

The syntax to export a const int is:
extern const int TILESIZE = 10;
however, I advise against doing this, even though it fixes your problem.

...

Practically, you should simply move the
const int TILESIZE = 10;
into the header file, instead of "extern const int TILESIZE" -- it will have static linkage, and be turned into a constant wherever it is used and the optimizer works on it.
Another code-style comment.

You might want to write your switch/case statements as follows:

switch(x) {  case 1: {    printf("%s\n", "X has value 1");  } break;  case 2: {    printf("%s\n", "X has value 2");  } break;  case 3:  case 4: {    printf("%s\n", "X has value 3 or 4");  } break;  default: {    printf("%s\n", "X is a chicken");  } break;}


It does a few things: it restricts the scope of variable declairations to be within one case label, and the visual pattern makes it clear that "rollthrough" won't be happening.

Next, a cute trick to create "real integer constants" is to use an anonymous enum:

enum { TILESIZE=10 };


This has the wonderful property that you can't take the address of an enum token (and then accidentally change it!). :) Plus, the size of a tile becomes "truely compile-time" (it is allowed in some different syntaxes than a static const int is).

This topic is closed to new replies.

Advertisement