我终于在链表中添加了添加功能,但无法在 main 中使用。 :(
我终于制作了我的 func 但无法在我的 main 中使用它。编译器错误如下:
<块引用>无法将参数
1' 的
void add(Node*, Node*)'Node' 转换为
Node*' 到
有人可以帮我解决该错误吗?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void add(Node* node, Node* newNode);
int main()
{
struct Node *llist;
struct Node *newNode;
newNode->data = 13;
llist = (Node*)malloc(sizeof(struct Node));
llist->data = 10;
llist->next = (Node*)malloc(sizeof(struct Node));
llist->next->data = 15;
llist->next->next = NULL;
add(llist,newNode);
printf("test\n");
struct Node *cursor = llist;
while (cursor != NULL)
{
printf("%d\n", cursor->data);
cursor = cursor->next;
}
system("pause");
return 0;
}
void add(Node* insertafter, Node* newNode)
{
newNode->next = insertafter->next;
insertafter->next = newNode;
}
I finally made my func but can't use it in my main. The compiler errors with:
cannot convert
Node' to
Node*' for argument1' to
void add(Node*, Node*)'
Can somebody help me resolve the error?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void add(Node* node, Node* newNode);
int main()
{
struct Node *llist;
struct Node *newNode;
newNode->data = 13;
llist = (Node*)malloc(sizeof(struct Node));
llist->data = 10;
llist->next = (Node*)malloc(sizeof(struct Node));
llist->next->data = 15;
llist->next->next = NULL;
add(llist,newNode);
printf("test\n");
struct Node *cursor = llist;
while (cursor != NULL)
{
printf("%d\n", cursor->data);
cursor = cursor->next;
}
system("pause");
return 0;
}
void add(Node* insertafter, Node* newNode)
{
newNode->next = insertafter->next;
insertafter->next = newNode;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该是
void add(struct Node* node, struct Node* newNode);
。OR:
另外,请注意,在为实际结构分配空间之前,您将值分配给作为指针的
newNode
的字段:还有一件事 - 如果这是 C而不是 C++,您应该删除
using namespace std;
It should be
void add(struct Node* node, struct Node* newNode);
.OR:
Also, please note that you asign values to fields of
newNode
which is a pointer, before allocating space for the actual struct:And one more thing - if this is C and not C++, you should remove
using namespace std;