【发布时间】:2021-03-08 17:12:50
【问题描述】:
结构的定义如下。
//Structure of the linked list node is as follows:
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
我必须完成我以这种方式完成的这个功能。我正在尝试使用函数定义中传递的 newData 参数创建一个节点。但它显示了我在下面附加的错误。
// 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;
}
我通过将 newData 作为参数传递给struct Node *newNode(newData); 创建节点时收到此错误
在函数Node* insertAtBegining(Node*, int):
prog.cpp:67:32: 错误:从 int 到 Node* [-fpermissive] 的无效转换
结构节点 *newNode(newData);
感谢您的帮助。
【问题讨论】:
-
顺便说一句,在 C++ 中,定义变量(或指针)时不需要
struct关键字。 -
你的功能在于。函数签名说函数返回
Node *,但你的函数什么也不返回。将返回类型更改为void(表示没有返回值)或返回Node *变量。
标签: c++ linked-list singly-linked-list