定义常量在visual studio中表现怪异
defined constants acting weird in visual studio
我使用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
变量,因为不需要语法糖。
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 什么时候在C++中返回常量引用是个好主意
- 代理对象的常量正确性
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 通过多个头文件使用常量变量
- Visual Studio 2017 中的 cpprestapi 问题找不到 POST/GET 常量
- 如何更改静态链接库中常量字符串数组的Visual Studio C++初始化顺序
- Visual Studio 2015:是一组允许的常量指针
- Visual Studio 2015字符串文字并不总是常量
- 如何在Visual Studio中使用#if指令编译时检查常量变量
- GNU 编译器与 Visual Studio 在分配的数组上分配了长度常量,在作用域中
- 在 Visual Studio 2012 中初始化矢量的静态常量向量
- Visual Studio,错误:在数组声明中使用 #define 常量时,应使用"]"
- Linux gcc和Windows Visual Studio处理静态常量的区别
- 如何在Visual Studio中初始化C++类中的静态常量浮点值
- gcc中类型的非常量引用的初始化无效,但Visual Studio中没有
- 定义常量在visual studio中表现怪异