Using Qt Creator C++ 11, nullptr is keyworded?
Asked Answered
N

2

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?

Neves answered 12/5, 2013 at 17:4 Comment(2)
Are you compiling as C++11? -std=c++11?Blown
It is only a warning to prevent users that the code might not work with C++11.Xenophanes
B
33

Open your .pro file from inside QtCreator and add this

QMAKE_CXXFLAGS += -std=c++0x
Byrann answered 12/5, 2013 at 17:9 Comment(11)
or -std=c++11 for GCC >= 4.7.Chockfull
@Chockfull 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.xByrann
CONFIG += c++11 for Qt 5Hunkers
@Hunkers now you are talking about the QT framework not about QtCreator, let's not mix stuff with different stuff.Byrann
@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.)Chockfull
@Chockfull 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.Byrann
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 flagsHunkers
@Hunkers 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.Byrann
How CONFIG variable is different from QMAKE_CXXFLAGS variable? Just think about it.Hunkers
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.Neves
(@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 so setting it as a default is not as trivial as it appears)Chockfull
F
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 */
Flashboard answered 3/2, 2020 at 5:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.