Question

I am using visual studio 2012 professional to make my first windows/directx program. At the top of my program, I have this:

3: #define SCREEN_HEIGHT 500;
4: #define SCREEN_WIDTH  400;

Before I decided to use constants this was working perfectly fine:

49: //set size but not coordinates. we'll do that when we create the window
50: RECT clientArea = {0, 0, 500, 400};
51: //x-coordinates, y-coordinates, height, width
52: 
53: //Makes the previous struct have the values for the client area not the window
54: AdjustWindowRect(&clientArea, WS_OVERLAPPEDWINDOW, FALSE);
55: //address of previously defined RECT, window style, do we have a menu?
56: 
57: //create the window and store the handle
58: windowHandle = CreateWindowEx(NULL,
59:                              "WindowClass1",                        //name of window class
60:                              "My First Windowed Program",           //title of window
61:                              WS_OVERLAPPEDWINDOW,                   //window style
62:                              400,                                   //x-position
63:                              200,                                   //y-position
64:                              clientArea.right - clientArea.left,    //width
65:                              clientArea.bottom - clientArea.top,    //height
66:                              NULL,                                  //No parent
67:                              NULL,                                  //We dont have any menu's
68:                              whichInstance,                         //instance handle
69:                              NULL);                                 //we only have one window
70: 
71: //display the window
72: ShowWindow(windowHandle, howToShowTheWindow);
73: //struct with window information, defined by windows in WinMain.

but when I change this line:

50: RECT clientArea = {0, 0, SCREEN_HEIGHT, SCREEN_WIDTH};

It gives me about thirty different errors. I'm pretty sure only the first few are relavent, and the rest are because those lines of code didn't work correctly..

1>c:\users\kenneth\documents\visual studio 2012\projects\my first directx program\my first directx program\my first dirextx program.cpp(50): error C2143: syntax error : missing '}' before ';'
1>c:\users\kenneth\documents\visual studio 2012\projects\my first directx program\my first directx program\my first dirextx program.cpp(50): error C2143: syntax error : missing ';' before ','
1>c:\users\kenneth\documents\visual studio 2012\projects\my first directx program\my first directx program\my first dirextx program.cpp(54): error C2065: 'clientArea' : undeclared identifier
1>c:\users\kenneth\documents\visual studio 2012\projects\my first directx program\my first directx program\my first dirextx program.cpp(54): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kenneth\documents\visual studio 2012\projects\my first directx program\my first directx program\my first dirextx program.cpp(54): error C2365: 'AdjustWindowRect' : redefinition; previous definition was 'function'

If I understand defined constants correctly, the pre-processor simply swaps them out for their values before any compiling is done, so this is very confusing.

Was it helpful?

Solution

Remove the semicolons:

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

Think of macros as more of "copy and paste" action. 500;, etc. was being copied and pasted into like RECT clientArea = {0, 0, 500;, 400;};

Also, why make the screen size fixed? It makes it much more difficult to change code in the future. I would declare a class and specify the screen size in the constructor:

class BasicWindow
{
    BasicWindow(std::size_t width, std::size_t height)
    : WndWidth_(width), WndHeight_(height){};
private:
    std::size_t WndWidth_;
    std::size_t WndHeight_;
};

OTHER TIPS

The syntactical problem is the extra semicolons, should be

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

But don't do that.

Macros are evil. You risk inadvertent, undesired text substitution. Well, you already had that, but worse.

Instead, define constants, like this:

int const screen_height = 500;
int const screen_width  = 400;

The #define approach is for 1970's C, it's a bit outdated (and risky, and an eyesore) as of 2013.

That's because you did not declare variables but rather macros. Thus, it syntaxically replace the symbols by what you said. And you included ; in your symbols, so the syntax is incorrect. Replace with:

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

This is an example of why you should avoid using macros. Prefer const variables, because they do not have any such side effet: they do not incur changes in syntax, whereas macros actually modifies the code before it is sent to the compiler.

Namely, wherever the preprocessor sees the symbol you define in your code, it replaces the symbol with exactly what you said (just as if you had done a replace operation by hand in your file). And this happens before the compiler even sees the code. Thus, when you use macros, you can have cryptic messages since the compiler sees another code than the one you have in front of you.

On the other hand, macros can be very useful to avoid boilerplate code. But you really have to know what you do. And here, you should definitely use const variables since there is no need for syntaxic sugar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top