8

I tried to implement BST but std::nullptr shows me an error:

error: expected unqualified-id before ‘nullptr’

#include <iostream>
#include <memory>

template <typename T>
class BinTreeNode {
public: 
    BinTreeNode(T key): data {key} {
        left = std::nullptr;
        right = std::nullptr; 
    }
    ~BinTreeNode() {}
    T data;
    BinTreeNode<T>* left;
    BinTreeNode<T>* right;
};

template <typename T>
class BinTree {
public:
    BinTree(){ 
        root = std::nullptr;
    }
    ~BinTree() {}
    BinTreeNode<T>* root;
};

int main(){
    BinTree<int> tree;
}

I have tried searching it up in Google but nothing meaningful came up related to std::nullptr

2
  • 1
    Please don't edit "Solved" into question titles. Instead, accept the answer that was helpful to you (click the green check mark below the number of votes it has received).
    – R_Kapp
    Commented Dec 23, 2015 at 18:23
  • 1
    Almost a dupe: stackoverflow.com/q/21510121/179910 Commented Dec 23, 2015 at 18:36

1 Answer 1

16

nullptr, unlike nullptr_t, does not need std:: prefix. The reason for this is that nullptr is a literal, and is not defined in namespace std, same way 42 is not defined there. Nor it requires any include directives to be used.

On the other hand, nullptr_t is a type, defined in namespace std, and requires <cstddef> to be included.

0