Search code examples
c++windowsvisual-studio-2012visual-c++-2012

defined constants acting weird in visual studio


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.


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