13

I'm using C++11 using Qt Creator.

"warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]"
"error: 'nullptr' was not declared in this scope"

This is on code that works elsewhere, the relevant part being:

... = nullptr;

What might be the problem?

Is this not already a keyword, isn't it's scope global?

Mat
  • 202,337
  • 40
  • 393
  • 406
alan2here
  • 3,223
  • 6
  • 37
  • 62

2 Answers2

33

Open your .pro file from inside QtCreator and add this

QMAKE_CXXFLAGS += -std=c++0x
user2348816
  • 532
  • 4
  • 9
  • 1
    or `-std=c++11` for GCC >= 4.7. – Mat May 12 '13 at 17:09
  • @Mat AFAK it triggers the same set of functions no matter what flag you are using, but `c++0x` it's also compatible with older version like gcc 4.6.x – user2348816 May 12 '13 at 17:11
  • 4
    `CONFIG += c++11` for `Qt 5` – Lol4t0 May 12 '13 at 17:13
  • @Lol4t0 now you are talking about the QT framework not about QtCreator, let's not mix stuff with different stuff. – user2348816 May 12 '13 at 17:15
  • @user2348816: your last comment makes no sense, Qt Creator uses qmake from the Qt framework as build system. The `.pro` file is a qmake configuration file. (Oh and it's Qt, not QT.) – Mat May 12 '13 at 17:16
  • @Mat you can use QtCreator and qmake without involving the QT libraries, they are 2 different things, QtCreator is a good, standalone, IDE for generic C++ development. – user2348816 May 12 '13 at 17:17
  • 1
    If talk about `.pro` file, then you use `qmake`. It is not even important, if you use Qt libraries, as `qmake` will generate `MakeFile` and `CONFIG` or `QMAKE_CXXFLAGS` are `qmake` (build system) level flags, not `Qt library` level flags – Lol4t0 May 12 '13 at 17:18
  • @Lol4t0 all I know is that the OP it's not even mentioning the QT framework but only QtCreator, so you are giving a nice advice when it's not needed. – user2348816 May 12 '13 at 17:19
  • 1
    How `CONFIG` variable is different from `QMAKE_CXXFLAGS` variable? Just think about it. – Lol4t0 May 12 '13 at 17:21
  • This fixed the project allowing it to run and seems as if this should be set by default, especially on new projects. Whats more the error given didn't help. Otherwise so far QTCreator seems great. – alan2here May 12 '13 at 17:22
  • (@alan2here: you'd have found the solution if you had typed that "unhelpful" comment in your favorite search engine, & C++11 introduces some [breaking changes](http://stackoverflow.com/questions/6399615/what-breaking-changes-are-introduced-in-c11) so setting it as a default is not as trivial as it appears) – Mat May 12 '13 at 17:28
0

Try adding the below lines to your cpp source code

#ifndef _GXX_NULLPTR_T
#define _GXX_NULLPTR_T
   typedef decltype(nullptr) nullptr_t;
#endif 
/* C++11 */
U13-Forward
  • 69,221
  • 14
  • 89
  • 114