13

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

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 IntelliSense in Visual Studio 2008. Can this be repaired or replaced?

karel
  • 5,489
  • 46
  • 45
  • 50
VIclean
  • 155
  • 1
  • 2
  • 7
  • 4
    `nullptr` is a newer language feature and it looks like your compiler just doesn't support it. The old was of doing this was to check against 0 but that's not quite as typesafe. Also you might want to read this http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr – shuttle87 Jun 26 '14 at 14:41
  • 2
    You can only use `nullptr` in Visual Studio 2010 and newer, it is a C++11 feature. – Cory Kramer Jun 26 '14 at 14:43
  • What's the difference between intelisense in VS2008 and VS2010? – Holt Jun 26 '14 at 14:45
  • You can upgrade to VS2010, how/why would that impact your usage of Intellisense? – Chris O Jun 26 '14 at 14:45
  • You know that VS2008 does not support `nullptr`. You know the solution is to use a newer compiler. And yet you want to somehow find a way to change the compiler without changing compiler. – David Heffernan Jun 26 '14 at 14:46

3 Answers3

13

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.

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

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

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.

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){
}

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++?

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.

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
7

nullptr is part of C++11, in C++03 you simply use 0:

if (!Data)
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

When it's not recommended to edit sources, you can just give your c++ compiler appropriate define. For example, in CMakeLists.txt I've added line:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dnullptr=0")

And everything worked well

PolyGlot
  • 740
  • 6
  • 11