简体   繁体   English

为什么不能定义这些宏名称?

[英]Why can't I define these macro names?

If I make a define at the command like -Dfirst and -Dsecond I get a bunch of errors: 如果我在-Dfirst-Dsecond类的命令上进行定义, -Dsecond得到很多错误:

In file included from main.cpp:1:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/iostream:39:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/ostream:38:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/ios:40:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/char_traits.h:39:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_algobase.h:64:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_pair.h:101:11: error: expected member name or ';' after declaration specifiers
      _T1 first;                 /// @c first is a copy of the first object
      ~~~ ^
<command line>:1:15: note: expanded from here
#define first 1
              ^
...
              ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

But if I use capital FIRST and SECOND it's fine. 但是,如果我使用大写FIRSTSECOND就可以了。

If you define first and second before including anything, that includes std::pair inside <algorithm> , it will screw your code, because the variables inside std::pair are called first and second , and your macro is defined to that, so it expands the names to the expression they are defined to, making your code malformed. 如果您在包含任何内容(包括<algorithm>内的std::pair之前定义了first和second,它将使您的代码搞砸,因为std::pair内的变量被称为firstsecond ,因此您的宏被定义了,因此它将名称扩展为它们所定义的表达式,从而使您的代码格式错误。

Basically, its the same as doing 基本上和做的一样

#define A 1

class A{
};

This will also not compile, because A gets converted to 1, and 1 is not valid class name 这也不会编译,因为A转换为1,而1不是有效的类名

如您所见,first和second在整个STL中都使用,因此,通过将它们定义为没有,您将使其变得不可解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM