Node* node;
*node = nullptr;
Report an error:
error: no viable overloaded '='
*node = nullptr;
but
Node* node;
*node = NULL;
is correct ?
Node* node;
*node = nullptr;
Report an error:
error: no viable overloaded '='
*node = nullptr;
but
Node* node;
*node = NULL;
is correct ?
Most likely a Node
can be constructed with an int
. Since null is defined as an int constant equal to 0
, so you wrongly call the node constructor and assign it to the node.
As Sid S said, node = nullptr
is the right expression.
NULL
is most likely an old macro, inherited from the C world, and defined 0.
So what is happening in the second case is assigning the number 0 to a variable of type Node
. Whereas in the first case a pointer (nullptr
) is assigned.
The compiler knows for sure that this is wrong, since *node
is not a pointer. Which means it will complain.
Whether it will complain for assigning a numeric 0 to Node
will depend if the assignment operator for Node
has been overloaded to accept a number. Or whether there is an implicit conversion/constructor from a number to Node
.
Neither one is correct. You should use node = nullptr;
to assign to the node
variable, as opposed to assigning to what node
is pointing to.
So:
Node* node;
*node = nullptr;
cannot be done because, you are trying to assign pointer to type Node
, what you should do is:
Node* node;
node = nullptr;
You have to understand when you declare pointer you use following sintax:
int *n = 5;
n = nullptr;
where as following is
int *n = 5;
*n = 3;
*n
is de-referencing pointer, and going to the address n
points to. Same is true with in your Node
example. Meanwhile *node = NULL
might be implementation dependent. So Node
type might have something like this:
Node& operator = (const Node &n ) {
if (n == NULL) {
// do something
}
// do something else
return n;
}
You should use node=nullptr;
You have made a pointer and now have to allocated the pointer to point to something, therefore node=nullptr;
Node * nullptr; defines the type of the variable nullptr is , it is a pointer to type node