user16672266
user16672266

Reputation:

nullptr as undeclared type

I'm trying to practice the null pointer in C++, and I got nullptr as undeclared identifier. could you please help me and tell me what is wrong ?

here is my code:

#include <iostream>
#include <cstddef>

void print(int x)
{
    std::cout << "print(int): " << x << '\n';
}

void print(int* x)
{
    if (!x)
        std::cout << "print(int*): null\n";
    else
        std::cout << "print(int*): " << *x << '\n';
}

int main()
{
    int* x = nullptr;
    print(x); // calls print(int*)

    print(nullptr);

    return 0;
}

thank you for your help :)

Upvotes: 1

Views: 538

Answers (1)

Gabi5537
Gabi5537

Reputation: 101

Try to compile with flag like:

>> g++ -std=c++11 fileName.cpp -o whatNameYouWant

Upvotes: 2

Related Questions