C++ visual studio中行为怪异的已定义常量
我正在使用visual studio 2012 professional制作我的第一个windows/directx程序。在我的程序顶部,我有以下内容:C++ visual studio中行为怪异的已定义常量,c++,windows,visual-studio-2012,visual-c++-2012,C++,Windows,Visual Studio 2012,Visual C++ 2012,我正在使用visual studio 2012 professional制作我的第一个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 clientA
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};
它给了我大约三十个不同的错误。我很确定只有前几行是相关的,其余的都是因为那些代码行工作不正常
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'
如果我正确理解定义的常量,预处理器只需在编译之前将它们换成它们的值,因此这非常混乱。删除分号:
#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH 400
将宏更多地视为“复制和粘贴”操作<代码>500代码>,等等被复制并粘贴到likeRECT clientrea={0,0500;,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_;
};
这是因为您没有声明变量,而是声明宏
。因此,它在句法上用你所说的替换符号。你包括了代码>在符号中,因此语法不正确。替换为:
#定义屏幕高度500
#定义屏幕宽度400
这就是为什么应该避免使用宏的一个示例。首选const
变量,因为它们没有任何这样的副作用:它们不会引起语法上的更改,而宏实际上会在代码发送到编译器之前修改代码
也就是说,无论预处理器在哪里看到您在代码中定义的符号,它都会用您所说的内容替换符号(就像您在文件中手动执行了replace
操作一样)。这发生在编译器看到代码之前。因此,当您使用宏时,可能会有一些隐藏的消息,因为编译器看到的代码与您面前的代码不同
另一方面,宏对于避免样板代码非常有用。但是你必须知道你在做什么。在这里,你肯定应该使用const
变量,因为不需要合成糖。句法问题是额外的分号,应该是
#define SCREEN_HEIGHT 500
#define SCREEN_WIDTH 400
但是不要那样做
宏是邪恶的。您可能会遇到意外的文本替换。你已经有了,但更糟
而是定义常量,如下所示:
int const screen_height = 500;
int const screen_width = 400;
#define
方法适用于20世纪70年代的C语言,从2013年开始就有点过时(而且有风险,而且很碍眼)。简单地#define
这些宏没有分号。我们需要一个简单的答案来回答这个问题。这是一个无数次的重复,我只是按照一个教程。我已经为linux做了一些命令行应用程序,但还没有GUI或任何东西。我应该知道这里不应该有分号。感谢you@kennycoc:有关更现代的方法,请参阅。您正在使用的教程可能很旧。谢谢。他们在switch语句的下方也有代码,而不是仅仅使用default:
,这可能会提醒我这不是最好的站点