C++ 在用于定义数字的数字常量之前需要未限定的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27524755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected unqualified-id before numeric constant for defining a number
提问by Kil'jaeden
I'm new to C++, so I don't know what they mean with this error in a phidget-code example:
我是 C++ 的新手,所以我不知道他们在 phidget 代码示例中的这个错误是什么意思:
Main.cpp:8:16: error: expected unqualified-id before numeric constant
Main.cpp:8:16: 错误:数字常量前的预期未限定 ID
//verander de volgende informatie naar de informatie voor jouw database
#define dserver "oege.ie.hva.nl"
#define duser "username"
#define dpassword "password"
#define ddatabase "databasename"
#define homeid 1234 //line 8
Is there a syntax error? Or something else? I use #define instead of int.
是否有语法错误?或者是其他东西?我使用 #define 而不是 int。
EDIT: added full error log..
编辑:添加完整的错误日志..
complete error-log: http://pastebin.com/3vtbzmXD
Full main.cpp code: http://pastebin.com/SDTz8vni
完整的错误日志:http: //pastebin.com/3vtbzmXD
完整的 main.cpp 代码:http: //pastebin.com/SDTz8vni
采纳答案by Mike Seymour
The full error is
完整的错误是
error: expected unqualified-id before numeric constant
note: in expansion of macro ‘homeid'
string homeid;
^
You're trying to declare a variable with the same name as a macro, but that can't be done. The preprocessor has already stomped over the program, turning that into string 1234;
, which is not a valid declaration. The preprocessor has no knowledge of the program structure, and macros don't follow the language's scope rules.
您试图声明一个与宏同名的变量,但这无法完成。预处理器已经对程序进行了处理,将其转换为string 1234;
,这不是有效的声明。预处理器不了解程序结构,并且宏不遵循语言的范围规则。
Where possible, use language features like constants and inline functions rather than macros. In this case, you might use
在可能的情况下,使用常量和内联函数等语言特性,而不是宏。在这种情况下,您可能会使用
const int homeid = 1234;
This will be scoped in the global namespace, and can safely be hidden by something with the same name in a narrower scope. Even when hidden, it's always available as ::homeid
.
这将被限定在全局命名空间中,并且可以安全地在更窄的范围内被同名的东西隐藏。即使隐藏,它也始终以::homeid
.
When you really need a macro, it's wise to follow the convention of using SHOUTY_CAPS
for macros. As well as drawing attention to the potential dangers and wierdnesses associated with macro use, it won't clash with any name using other capitalisation.
当你真的需要一个宏时,遵循 using SHOUTY_CAPS
for 宏的约定是明智的。除了提请注意与宏使用相关的潜在危险和奇怪之处外,它不会与使用其他大写的任何名称发生冲突。
回答by Mike Seymour
That line is fine.
那条线没问题。
What is most likely happening is that the compiler is complaining not about the macro definition itself, but about the useof the macro. Example:
最有可能发生的是编译器抱怨的不是宏定义本身,而是宏的使用。例子:
#define homeid 1234
void homeid() {
}
When compiling this with GCC, I get:
用 GCC 编译时,我得到:
so.cc:1:16: error: expected unqualified-id before numeric constant #define homeid 1234 ^ so.cc:3:6: note: in expansion of macro ‘homeid' void homeid() { ^
This tells you that the numeric constant prompting the complaint is part of the macro definition, but also that that macro is used(in this case seemingly by accident) on line 3. Take a look at where the macro expansion is coming from in your code.
这告诉您,提示投诉的数字常量是宏定义的一部分,而且在第 3 行使用了该宏(在本例中似乎是偶然的)。看看代码中宏扩展的来源.