定义常量在visual studio中表现怪异

defined constants acting weird in visual studio

本文关键字:studio 常量 visual 定义      更新时间:2023-10-16

我使用visual studio 2012专业制作我的第一个windows/directx程序。在我的程序的顶部,我有这个:

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

在我决定使用常量之前,这工作得很好:

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.

但是当我改变这一行时:

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

它给了我大约30个不同的错误。我很确定只有前几行是相关的,其余的都是因为那些行代码没有正确工作。

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

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

去掉分号:

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

把宏看作更多的"复制和粘贴"操作。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_;
};

语法问题是多余的分号,应该是

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

但是不要那样做

宏是邪恶的。您可能会无意中出现不希望的文本替换。好吧,你已经有了,但更糟。

相反,定义常量,像这样:

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

#define方法适用于1970年代的C,它在2013年有点过时(而且有风险,而且很碍眼)。

这是因为您没有声明变量,而是声明macros。因此,它在语法上用您所说的内容替换符号。并且您在符号中包含了;,因此语法不正确。替换为:

#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH  400

这是一个应该避免使用宏的例子。首选const变量,因为它们没有任何副作用:它们不会导致语法变化,而宏实际上在将代码发送给编译器之前修改了代码。

也就是说,无论预处理器在哪里看到您在代码中定义的符号,它都将该符号替换为您所说的符号(就像您在文件中手工完成了replace操作一样)。这发生在编译器看到代码之前。因此,当您使用宏时,由于编译器看到的是您面前的代码之外的其他代码,因此可能会有神秘的消息。

另一方面,宏对于避免样板代码非常有用。但你必须知道你在做什么。在这里,您肯定应该使用const变量,因为不需要语法糖。