C++ 编译错误 'nullptr' 未声明的标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24433436/
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
Compile error 'nullptr' undeclared identifier
提问by VIclean
I'm tring to compile a source with Visual Studio 2008 Express but I'm getting this error:
我正在尝试使用 Visual Studio 2008 Express 编译源代码,但出现此错误:
Error C2065: 'nullptr' undeclared identifier.
My code:
我的代码:
if (Data == nullptr)
{
show("Data is null");
return 0;
}
I read on google that I should upgrade to Visual Studio 2010, but I don't want to do this because of the 2008 intelisense. Can this be repaired or replaced?
我在谷歌上读到我应该升级到 Visual Studio 2010,但由于 2008 智能感知,我不想这样做。这可以修理或更换吗?
回答by shuttle87
The error you are getting is because the compiler doesn't recognize the nullptr
keyword. This is because nullptr
was introduced in a later version of visual studio than the one you are using.
您遇到的错误是因为编译器无法识别nullptr
关键字。这是因为它nullptr
是在比您使用的版本更高的 Visual Studio 版本中引入的。
There's 2 ways you might go about getting this to work in an older version. One idea comes from Scott Meyers c++ book where he suggests creating a header with a class that emulates nullptr
like this:
有两种方法可以让它在旧版本中工作。一个想法来自 Scott Meyers c++ 一书中,他建议创建一个带有如下类模拟的标题nullptr
:
const // It is a const object...
class nullptr_t
{
public:
template<class T>
inline operator T*() const // convertible to any type of null non-member pointer...
{ return 0; }
template<class C, class T>
inline operator T C::*() const // or any type of null member pointer...
{ return 0; }
private:
void operator&() const; // Can't take address of nullptr
} nullptr = {};
This way you just need to conditionally include the file based on the version of msvc
这样你只需要根据 msvc 的版本有条件地包含文件
#if _MSC_VER < 1600 //MSVC version <8
#include "nullptr_emulation.h"
#endif
This has the advantage of using the same keyword and makes upgrading to a new compiler a fair bit easier (and please do upgrade if you can). If you now compile with a newer compiler then your custom code doesn't get used at all and you are only using the c++ language, I feel as though this is important going forward.
这具有使用相同关键字的优点,并使升级到新编译器更容易(如果可以,请升级)。如果您现在使用较新的编译器进行编译,那么您的自定义代码根本不会被使用,而您只使用 C++ 语言,我觉得这对未来很重要。
If you don't want to take that approach you could go with something that emulates the old C style approach (#define NULL ((void *)0)
) where you make a macro for NULL
like this:
如果您不想采用这种方法,则可以使用模拟旧 C 风格方法 ( #define NULL ((void *)0)
) 的方法,在其中创建一个宏,NULL
如下所示:
#define NULL 0
if(data == NULL){
}
Note that this isn't quite the same as NULL
as found in C, for more discussion on that see this question: Why are NULL pointers defined differently in C and C++?
请注意,这与NULL
在 C 中发现的不完全相同,有关更多讨论,请参阅此问题:为什么 NULL 指针在 C 和 C++ 中定义不同?
The downsides to this is that you have to change the source code and it is not typesafe like nullptr
. So use this with caution, it can introduce some subtle bugs if you aren't careful and it was these subtle bugs that motivated the development of nullptr
in the first place.
这样做的缺点是您必须更改源代码,而且它不像nullptr
. 所以谨慎使用它,如果你不小心,它可能会引入一些微妙的错误,正是这些微妙的错误首先激发了开发nullptr
。
回答by Paul Evans
nullptr
is part of C++11, in C++03 you simply use 0
:
nullptr
是 C++11 的一部分,在 C++03 中,您只需使用0
:
if (!Data)