nullptr is C++ only; it is not needed in C because in C ((void*)0) is convertible to any other pointer type without casts.
If you really really really like to type nullptr in C, you can use
#define nullptr ((void*)0)
and it would then work mostly the same.
Notice that C has the NULL macro from <stddef.h>; it is readable too, but its expansion is implementation-defined, so it might be either ((void*)0) or 0 (or something really strange); if it expands to 0, you wouldn't get any diagnostics from
int a = NULL;
Answer from Antti Haapala -- Слава Україні on Stack OverflowThe 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:
Copyconst // 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
Copy#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:
Copy#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.
nullptr is part of C++11, in C++03 you simply use 0:
Copyif (!Data)
c++ - Why is NULL undeclared? - Stack Overflow
[coap] use of `nullptr` in C API
g++ - C++ Error 'nullptr was not declared in this scope' in Eclipse IDE - Stack Overflow
"use of undeclared identifier nullptr" everywhere 'nullptr' is used
NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.
In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).
Otherwise, add
#include <stddef.h>
to get the NULL definition.
Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.
There are problems with using 0 (and hence NULL). For example:
void f(int);
void f(void*);
f(0); // Ambiguous. Calls f(int).
The next version of C++ (C++0x) includes nullptr to fix this.
f(nullptr); // Calls f(void*).
According to the GCC page for C++11:
To enable C++0x support, add the command-line parameter -std=c++0x to your g++ command line. Or, to enable GNU extensions in addition to C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7 and later support -std=c++11 and -std=gnu++11 as well.
Did you compile with -std=gnu++0x ?
Finally found out what to do. Added the -std=c++0x compiler argument under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. It works now!
But how to add this flag by default for all C++ projects? Anybody?
I use the C/C++ extension for VSCode and I wanted to give a shot to c23, gcc does recognize nullptr but the VSCode IDE doesn't, and even tho my code works, I dont like the error message in the IDE. Any solutions??
I'm making a basic stack class. But I haven't used generics before, and I'm seeing all sorts of weird errors when I try to compile.
Stack.h:
//My name
#include "StackNode.h"
template <class T> class Stack {
public:
void push(T const&);
T pop();
T peek() const;
void clear();
bool isEmpty() const;
private:
StackNode<T>* top;
void clearHelper(StackNode<T>* start);
};Stack.cpp:
//My name
#include "Stack.h"
template <class T> void Stack<T>::push(T const& val) {
StackNode<T>* newNode(val, top);
top = newNode;
}
template <class T> T Stack<T>::pop() {
if(top == nullptr) {
return nullptr;
}
T tempaT = top.getValue();
StackNode<T>* tempNode = top;
top = tempNode.getPrev();
delete tempNode;
return tempaT;
}
template <class T> T Stack<T>::peek() const {
if(top == nullptr) {
return nullptr;
} else {
return top.getValue();
}
}
void Stack<T>::clear() {
if(top == nullptr) {return;}
clearHelper(top);
top = nullptr;
}
void Stack<T>::clearHelper(StackNode<T>* start) {
if(start == nullptr) {
return;
} else {
clearHelper(start.getPrev());
delete start;
}
}
bool Stack<T>::isEmpty() const {
return top == nullptr;
}The errors are as follows. I can't even find other people with the nullptr error.
Stack.cpp:11:12: error: use of undeclared identifier 'nullptr'
if(top == nullptr) {
^
Stack.cpp:12:10: error: use of undeclared identifier 'nullptr'
return nullptr;
^
Stack.cpp:14:16: error: member reference base type 'StackNode<T> *' is not a structure or union
T tempaT = top.getValue();
~~~^~~~~~~~~
Stack.cpp:16:16: error: member reference base type 'StackNode<T> *' is not a structure or union
top = tempNode.getPrev();
~~~~~~~~^~~~~~~~
Stack.cpp:22:12: error: use of undeclared identifier 'nullptr'
if(top == nullptr) {
^
Stack.cpp:23:10: error: use of undeclared identifier 'nullptr'
return nullptr;
^
Stack.cpp:25:13: error: member reference base type 'StackNode<T> *const' is not a structure or union
return top.getValue();
~~~^~~~~~~~~
Stack.cpp:29:12: error: use of undeclared identifier 'T'
void Stack<T>::clear() {
^
Stack.cpp:29:16: warning: extra qualification on member 'clear' [-Wextra-qualification]
void Stack<T>::clear() {
^
Stack.cpp:29:16: error: out-of-line definition of 'clear' does not match any declaration in the global namespace
void Stack<T>::clear() {
^~~~~
Stack.cpp:30:5: error: use of undeclared identifier 'top'
if(top == nullptr) {return;}
^
Stack.cpp:30:12: error: use of undeclared identifier 'nullptr'
if(top == nullptr) {return;}
^
Stack.cpp:31:14: error: use of undeclared identifier 'top'
clearHelper(top);
^
Stack.cpp:32:2: error: use of undeclared identifier 'top'
top = nullptr;
^
Stack.cpp:32:8: error: use of undeclared identifier 'nullptr'
top = nullptr;
^
Stack.cpp:35:12: error: use of undeclared identifier 'T'
void Stack<T>::clearHelper(StackNode<T>* start) {
^
Stack.cpp:35:38: error: use of undeclared identifier 'T'
void Stack<T>::clearHelper(StackNode<T>* start) {
^
Stack.cpp:35:38: error: use of undeclared identifier 'T'
Stack.cpp:35:16: warning: extra qualification on member 'clearHelper' [-Wextra-qualification]
void Stack<T>::clearHelper(StackNode<T>* start) {
^
Stack.cpp:36:14: error: use of undeclared identifier 'nullptr'
if(start == nullptr) {
^
Stack.cpp:44:12: error: use of undeclared identifier 'T'
bool Stack<T>::isEmpty() const {
^
Stack.cpp:44:16: warning: extra qualification on member 'isEmpty' [-Wextra-qualification]
bool Stack<T>::isEmpty() const {
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
3 warnings and 20 errors generated.Hello,
I'm writing a program in which it generates a random number between 0-100 and the user has to guess. The program will respond with whether or not their guess is correct, too high, or too low, etc. However, in putty my source code is not compiling with the error lab2.cpp:13: error: 'nullptr' was not declared in this scope lab2.cpp:35: error: expected ';' before '}' token
My code is as follows:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int n = 0;
bool do_more = true;
srand(time(nullptr));
int target = rand() % 100 + 1;
cout << "Welcome to the number guessing game! " << endl;
cout << "Please enter your guess, between 0 and 100. " << endl;
do {
cout << "Enter your guess: ";
cin >> n;
if (n == 0) {
cout << "Bye! ";
do_more = false;
}
else if (n > target) {
cout << "Guess is too high! Try again! " << endl;
}
else if (n < target) {
cout << "Guess is too low. Try again! " << endl;
}
else {
cout << "You win! ";
cout << "Answer is " << n << endl;
do_more = false;
}
} while (do_more);
return 0;
}Thanks in advance for any help with this confusing error.
Sorry if this question is really simple, but I've been trying to find out if (x > x), (x < x), (x >= x) and (x <= x) where one x is nullptr and the other x is a valid pointer is undefined behavior. I've been trying to test it with "-fsanitize=undefined" and it hasn't complained so far. So I just wanted some confirmation from people who know way more than me.
So for example, does this test code invoke undefined behavior?
#include <iostream>
struct CheckStatus
{
bool first = false;
bool second = false;
bool third = false;
bool fourth = false;
};
[[nodiscard]] CheckStatus Check()
{
CheckStatus cs;
char c1('a');
char* cp1(&c1);
char* cp2(nullptr);
if(cp1 < cp2) cs.first = true;
if(cp1 > cp2) cs.second = true;
if(cp1 <= cp2) cs.third = true;
if(cp1 >= cp2) cs.fourth = true;
return cs;
}
int main()
{
CheckStatus cs(Check());
if(cs.first) std::cout << "1st: true" << std::endl;
if(cs.second) std::cout << "2nd: true" << std::endl;
if(cs.third) std::cout << "3rd: true" << std::endl;
if(cs.fourth) std::cout << "4th: true" << std::endl;
return 0;
}(In case for some reason the code isn't formatted correctly for you, please see it here: "https://pastebin.com/nNetU7pu")