定义的常量在视觉工作室中表现得很奇怪

iCo*_*ime 0 c++ windows visual-studio-2012 visual-c++-2012

我正在使用visual studio 2012 professional来制作我的第一个windows/directx程序.在我的程序的顶部,我有这个:

3: #define SCREEN_HEIGHT 500;
4: #define SCREEN_WIDTH  400;
Run Code Online (Sandbox Code Playgroud)

在我决定使用常量之前,这非常合适:

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.
Run Code Online (Sandbox Code Playgroud)

但是当我改变这一行时:

50: RECT clientArea = {0, 0, SCREEN_HEIGHT, SCREEN_WIDTH};
Run Code Online (Sandbox Code Playgroud)

它给了我大约三十种不同的错误.我很确定只有前几个是相关的,其余的是因为那些代码行无法正常工作..

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'
Run Code Online (Sandbox Code Playgroud)

如果我正确理解了定义的常量,预处理器只需在完成任何编译之前将它们换成它们的值,所以这非常令人困惑.

Jes*_*ood 5

删除分号:

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400
Run Code Online (Sandbox Code Playgroud)

将宏视为"复制和粘贴"操作的更多内容.500;正在被复制和粘贴等RECT clientArea = {0, 0, 500;, 400;};

另外,为什么屏幕尺寸固定?这使得将来更改代码变得更加困难.我会声明一个类并在构造函数中指定屏幕大小:

class BasicWindow
{
    BasicWindow(std::size_t width, std::size_t height)
    : WndWidth_(width), WndHeight_(height){};
private:
    std::size_t WndWidth_;
    std::size_t WndHeight_;
};
Run Code Online (Sandbox Code Playgroud)