-1

Many other questions address similar issues, but I have yet to find an answer to this specific issue. I am getting the following error:

error: ‘nullptr’ was not declared in this scope

When compiling with the following command:

g++ -std=gnu++0x file1.cpp file2.cpp file3.cpp file4.cpp -o test.out

g++ version is 4.4.7, and unfortunately cannot be updated. Here is my system information:

LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: RedHatEnterpriseWorkstation
Description:    Red Hat Enterprise Linux Workstation release 6.9 (Santiago)
Release:    6.9
Codename:   Santiago

Note that my program compiles fine on MacOS High Sierra version 10.13 with the -std=c++11 flag, which is not available on the Linux machine. I would prefer not to polyfill nullptr if possible.


Edit

I repeat, adding the -std=c++0x flag DOES NOT WORK. It works on MacOS, but not the Linux machine. That is the problem, and why this is not the same as similar questions I have seen on SO.

treyhakanson
  • 4,611
  • 2
  • 16
  • 33
  • Possible duplicate of [Can nullptr be emulated in gcc?](https://stackoverflow.com/questions/2419800/can-nullptr-be-emulated-in-gcc) – UnholySheep Mar 10 '18 at 19:23
  • Did you add flag `-std=c++0x` inside Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous? – Smit Ycyken Mar 10 '18 at 19:23
  • I'm compiling from the command line, not an IDE. That flag is being used though, as the question states. – treyhakanson Mar 10 '18 at 19:26
  • "g++ version is 4.4.7, and unfortunately cannot be updated". You can always update ;) You can install multiple versions in parallel and also in your home directory if you have no admin rights. – Klaus Mar 10 '18 at 19:26
  • 1
    Null pointer constant is available in GCC 4.6 and higher. https://gcc.gnu.org/projects/cxx-status.html – Smit Ycyken Mar 10 '18 at 19:27
  • 1
    @SmitYcyken: Please do not answer in the comments section. Thank you. – Lightness Races in Orbit Mar 10 '18 at 19:32
  • @Klaus: _"You can always update"_ That's rather short sighted I'm afraid. Many (most?) programmers are not simply doing it for fun on their home systems! – Lightness Races in Orbit Mar 10 '18 at 19:33
  • For future questions, could someone please explain why this is getting downvoted? – treyhakanson Mar 10 '18 at 19:33
  • I didn't downvote but you haven't done much research so that might be why – Lightness Races in Orbit Mar 10 '18 at 19:33
  • @LightnessRacesinOrbit thanks, I'll put in more work next time – treyhakanson Mar 10 '18 at 19:35
  • @treyhakanson: Good luck! – Lightness Races in Orbit Mar 10 '18 at 19:35
  • @LightnessRacesinOrbit: What is the problem by using multiple compiler installations in parallel. Takes 5 or maybe 10 minutes for configure/make/install... – Klaus Mar 10 '18 at 20:01
  • @Klaus: 5 or maybe 10 minutes for a compiler build? Hey, that would be nice! Regardless, time to build is not the point at all. You're making a lot of assumptions about control over the build environment, target environments, compatibility with other libraries/software, not to mention the reality that in business you need a very good reason to make changes like this (and having `nullptr` is not one of them) – Lightness Races in Orbit Mar 10 '18 at 20:04
  • @LightnessRacesinOrbit: I asked you already what the problem is! I can't see it. And I am wondering why others have such problems with very old compilers. So it would be nice to get an answer! – Klaus Mar 10 '18 at 20:07
  • 1
    @Klaus Explaining all the myriad reasons someone who programs in real life may be compiler-constrained is well out of the scope of a SO comment. You could ask a question on SoftwareEngineering.SE though. – Lightness Races in Orbit Mar 10 '18 at 20:10

4 Answers4

4

Looking at the feature list of GCC it clearly shows :

Null pointer constant | N2431 | GCC 4.6

Which means that nullptr was implemented in GCC 4.6. GCC 4.4.7 doesn't support it yet, no flag is going to fix that. You're going to have to update your compiler if you want to use it.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
3

Seems that nullptr is not supported with 4.4.7 but with GCC 4.6.0: http://gcc.gnu.org/gcc-4.6/changes.html You could try using NULL instead of nullptr

Werner
  • 281
  • 1
  • 12
2

The specific nature of the error (the fact that it thinks it is an identifier that needs to be defined, rather than a keyword) makes it clear that your version of GCC simply doesn't support that C++11 feature.

There is no way to deal with this that doesn't involve changing compilers. You cannot effectively emulate this feature (because if you could, it wouldn't have needed to be a language feature in the first place). So if you cannot update your compiler, you cannot run code that expects all of C++11 to be implemented.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

You can use Developer Toolset (DTS) to get a C++11 compiler which is compatible with the Red Hat Enterprise Linux 6 system libraries. The system compiler has very limited C++11 support only because it is too old—the first version of GCC 4.4 was released in 2009.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92