Expected unqualified identifier before 'nullptr

I tried to implement BST but it std::nullptrshows me an error:

error: expected unqualified-id to '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 tried to find it on Google, but nothing significant was connected with std::nullptr

+4
source share
1 answer

nullptr, unlike nullptr_t, the prefix is ​​not necessarystd:: . The reason for this is that nullptr is a literal and not defined in the namespace std, the same 42 is not defined there. Also, this does not require any include directives.

On the other hand, it nullptr_tis a type defined in a namespace stdand needs to be included <cstddef>.

+13
source

Source: https://habr.com/ru/post/1621510/


All Articles