I got this error and I dont know how to fix it.
error: must use 'struct' tag to refer to type 'node' in this scope
node *node = new node;
my code where the error is.
//New empty tree
struct node *newTreeNode(int data)
{
//New tree nodes
node *node = new node;
//New data node
node->data = data;
//New left node
node->left = nullptr;
//New right node
node->right = nullptr;
return node;
}
^
This error comes from the strangeness that is declaring an object with the same name as its type:
node *node = new node;
Not only is this extremely confusing to readers of your program, but now on the RHS the word node means the object, not the type. So new node becomes invalid.
The error message is kindly informing you that you can make node refer to the type by writing struct before it:
node* node = new struct node;
This works because, when T is a class type, struct T always means that class type T and cannot mean anything else.
But, honestly, simply do not do this. Use better names.
You have declared a variable called node. That is the name of a type you're intending to use after that declaration. So you need to specify that you're referring to the type, not the variable by using struct or class appropriately.
node *node = new struct node;
^^^^^^
The better solution would be to use a different name for the variable.
node* n = new node;
Related
The Definition of the structure is as follows.
//Structure of the linked list node is as follows:
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
I have to complete this function which I have completed this way. I am trying to create a Node using the newData parameter passed in the function definition. But it shows the error which I have attached below.
// function inserts the data in front of the list
Node* insertAtBegining(Node *head, int newData) {
//Your code here
struct Node* newNode(newData);
struct Node* temp;
temp=head;
head=newNode;
newNode->next=temp;
}
I get this error while I am create a node by passing newData as parameter to struct Node *newNode(newData);
In function Node* insertAtBegining(Node*, int):
prog.cpp:67:32: error: invalid conversion from int to Node* [-fpermissive]
struct Node *newNode(newData);
Thank You for your help.
Your constructor returns a Node, not a Node*, so when you try to initialize newNode, the compiler thinks you're trying to create a pointer using an int. Instead you should be expecting your constructor to give you a Node:
Node newNode(newData);
Your insertAtBegining() implementation needs to create a new Node object. You aren't doing that. Right after your teacher's Your code here comment, your attempt has defined a "Node pointer" variable (whose type is Node*), but your attempt hasn't initialized that to any Node object (data that would have been passed to an object's constructor isn't the same as the object itself).
Also, you don't need to keep repeating struct that way that us old guys used to do with old-fashioned C code.
Lastly, you seem to also want to return the list's new head node back to the function's caller, but are unclear how you want to achieve that. There are two ways. The way that your code seems to be leaning towards is returning the new head in the same head parameter. In that case, it should look like this:
void insertAtBegining(Node** head, int newData)
{
//Your code here
Node* newNode = new Node(newData);
Node* temp;
temp = *head;
*head = newNode;
newNode->next = temp;
}
(The head parameter could also have been a "reference to a Node*", instead of this "pointer to a Node*", by making appropriate changes to the code.)
The second option (which maintains your teacher's recommended function signature) is to return the new head via the function's return value:
Node* insertAtBegining(Node* head, int newData)
{
//Your code here
Node* newNode = new Node(newData);
newNode->next = head;
return newNode;
}
I am implementing a doubly linked list in C++ and I am trying to add a node.append(node2) method to the node class which will link a new node object (node2) to the previous one (node). I receive an expected primary-expression before ‘;’ token error on the line node->previous = Node;. How do I get the address of the object within its class definition so that I can assign it to node->previous?
class Node {
public:
int value;
Node *next;//points to next node.
Node *previous;//doubly linked list.
void append(Node *node) {
next = node;
node->previous = Node;
node->next = NULL;
}
};
Use the this pointer to access the address of your object in C++. Change the line node->previous = Node; to node->previous = this;.
For the node :
struct node
{ int data;
struct node *next;
};
node *tmp = new node;
tmp->data=1;
tmp->next = nullptr;
To add this we use:
tail->next=tmp;
tail=tail->next;
Why don't we write:
tail->next=tmp;
tail=tmp;
While the ultimate result is the same, the first expresses the intent – update tail to point to the newly-inserted last node – clearer.
The correctness of the second is much less obvious, and requires some thinking (and the context of the line before it) to figure out why anyone would point tail to a temporary node.
So I made a linked list class:
class node {
public:
string name;
node *next;
node(string init) {name = init; next = nullptr;
};
and then in main I created a node pointer and initialized the name of the node it points to.
node *root;
root->name = "Hello"; //Error
The compiler doesn't call this an error, but when I run the program it stops working at this line of code. Does anyone know why?
You need to allocate the a node object for root to point to.
node* root = new node;
But be careful, now you have to make sure to call delete on root at the right moment. You coudl simplify things by using a smart pointer, or an automatic storage object
node root;
root.name = "Hello";
Sorry if this question has been asked before. On my search through SO I didn't find one that asked what I wanted to know.
Basically, when I have this:
typedef struct node
{
int data;
node *node;
} *head;
and do node *newItem = new node;
I am under the impression that I am declaring and reserving space, but not defining, a pointer to struct node, is that correct?
So when I do
newItem->data = 100 and newItem->next = 0
I get confused. newItem = 0would declare what exactly? Both data and next? The object as a whole?
I'm especially confused when I use typedef. Which part is the macro? I assume node because that's how I call it, but why do I need it?
Finally, what happens when I do:
node *temp;
temp = new node;
temp = head->next;
head->next = newItem;
newItem->next = temp;
I mean, head->next is a pointer pointing to object newItem, so I assume not to newItem.data or next themselves. So how can I use an uninitialized pointer that I described above safely like here? is head now not pointing to an uninitialized pointer?
I am under the impression that I am
declaring and reserving space, but not
defining, a pointer to struct node, is
that correct?
No. You are declaring a pointer, allocating space on the stack for the pointer, and dynamically allocating storage for a node to it it.
Don't confuse yourself by writing stuff like this:
typedef struct node
{
int data;
node * next;
} *head;
The way to write the struct in C++ is:
struct node
{
int data;
node * next;
};
You can now create a pointer:
node * pnode;
which allocates storage for the pointer.
and you can dynamically allocate storage for a node, and make the pointer point to it:
pnode = new node;
or do it all in one:
node * pnode = new node;
Now when you say:
pnode->data = 10;
you are not allocating anything. You are assigning 10 to the member called data of the node instance pointed to by pnode. Of course, if you had given your node a constructor (which you should normally do), you could do it all in one:
struct node
{
int data;
node * next;
node( int n, node * np ) : data( n ), next( np ) {}
};
node * pnode = new node( 10, 0 );
When you define your struct as you did and call new like you did, what you're doing is:
allocate a new struct node on the heap.
allocate space on the stack for newItem and set its value to the address of the new struct you allocated.
You didn't set any values to any of the members of the new struct. If you want that to happen whenever you create a new instance of the struct, you need to define a constructor.
typedef struct node
{
int data;
node *node;
} *head;
This declares node as a struct and defines head as a synonym for node*, so head is a type and not an object.
This makes this illegal: temp = head->next; because -> is not something that you can apply to a type.
new node dynamically allocates a node object and returns a pointer to it. node *newItem = new node; assigns this pointer to newItem. Note, though, that newItem->node (node here is a pointer object and not the type node) is not initialized so is neither null nor points to a valid node object.
This is also illegal because node has no next member.
newItem->next = temp;
I suggest that you choose a naming convention that means that you keep your types and your variables separate. It is somewhat confusing.
node *newItem = new node;
You create:
a new node on the heap (which in your case contains uninitialized values because you omitted the ()
a pointer on the stack, which points to this new node.
newItem->data = 100
simply sets the data member of the newly allocated node to 100.