编译错误'nullptr'未声明的标识符 [英] Compile error 'nullptr' undeclared identifier

查看:2904
本文介绍了编译错误'nullptr'未声明的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Visual Studio 2008 Express编译源,但出现此错误:

I'm tring to compile a source with Visual Studio 2008 Express but I'm getting this error:

Error C2065: 'nullptr' undeclared identifier.

我的代码:

if (Data == nullptr) 
{
    show("Data is null");
    return 0;
}

我在Google上阅读了我应该升级到Visual Studio 2010的信息,但是由于2008 intelisense的缘故,我不想这样做.可以修理或更换吗?

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?

推荐答案

您收到的错误是因为编译器无法识别nullptr关键字.这是因为nullptr是在Visual Studio的更高版本中引入的,而不是您正在使用的版本.

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.

有两种方法可以使它在较旧的版本中工作.一个想法来自斯科特·迈耶斯(Scott Meyers)c ++书,他建议使用类似nullptr的类创建标头,如下所示:

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:

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 = {};

这样,您只需要根据msvc的版本有条件地包含文件

This way you just need to conditionally include the file based on the version of msvc

#if _MSC_VER < 1600 //MSVC version <8
     #include "nullptr_emulation.h"
#endif

这具有使用相同关键字的优势,并使升级到新的编译器更加容易(如果可以,请进行升级).如果您现在使用较新的编译器进行编译,则您的自定义代码根本不会使用,而您仅使用的是c ++语言,我觉得这很重要.

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风格方法(#define NULL ((void *)0))的方法,在这种方法中,您可以为NULL创建一个宏,如下所示:

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:

#define NULL 0

if(data == NULL){
}

请注意,这与C中的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++?

缺点是您必须更改源代码,并且它不像nullptr那样具有类型安全性.因此,请谨慎使用,如果您不小心,它可能会引入一些细微的错误,而这些细微的错误首先激发了nullptr的发展.

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'未声明的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆