dangerChihuahua007
dangerChihuahua007

Reputation: 20915

Why is the Visual Studio compiler saying I'm missing a semicolon in my C++ function?

Here's my function. The width and height variables are global integer variables defined above this function with values in the hundreds.

#define ORIGINAL_WIDTH 800;
#define ORIGINAL_HEIGHT 700;

void set_perspective(void) {
  int view_width, view_height;
  if (width < height) {
    view_width = width;
    view_height = (float) width * ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
  }
  else {
    view_width = (float) height * ORIGINAL_WIDTH / ORIGINAL_HEIGHT;
    view_height = height;
  }
}

My C++ compiler notes "error C2143: syntax error : missing ';' before '/'" on the lines:

view_height = (float) width * ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
and 
view_width = (float) height * ORIGINAL_WIDTH / ORIGINAL_HEIGHT;

Does this have to do with casting? Why am missing a semicolon somewhere? Thank you for your time.

Upvotes: 2

Views: 1247

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726969

This is because you have semicolons in your #define. This is how it should look:

#define ORIGINAL_WIDTH 800
#define ORIGINAL_HEIGHT 700

#define does a literal text substitution, so your line looks like this to the compiler:

view_height = (float) width * 800; / 700;;

Upvotes: 13

Andy Finkenstadt
Andy Finkenstadt

Reputation: 3587

Eschew macros (which are text substitutions) and use constants, and this problem can't happen.

static const int ORIGINAL_HEIGHT = 800;
static const int ORIGINAL_WIDTH = 700;

Upvotes: 8

Related Questions