无法使用类型为"结构节点 *"的左值初始化类型为"结构节点 *"的参数

  • 本文关键字:节点 结构 类型 初始化 参数 c++
  • 更新时间 :
  • 英文 :


我仍然不知道我的代码有什么问题。

我只想从linkedlist类调用insertInEmpty和insertAtBegin来传递指针last和某些整数。

我很困惑,需要一些帮助来纠正这个问题。。。我很感激你的帮助!!非常感谢。

struct Node *last = nullptr;
last = linkedlist::insertInEmpty(last,5);   <---- error on last parameter
last = linkedlist::insertAtBegin(last,4);   <---- error on last parameter

linkedlist.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class linkedlist
{
public:
struct Node;
static struct Node *insertInEmpty(struct Node *last, int new_data);
static struct Node *insertAtBegin(struct Node *last, int new_data);
};
#endif // LINKEDLIST_H

链接列表.cpp

#include "linkedlist.h"
struct Node
{
int data;
struct Node *next;
};
struct Node *insertInEmpty(struct Node *last, int new_data)
{
// if last is not null then list is not empty, so return
if (last != nullptr)
return last;
// allocate memory for node
struct Node *temp = new Node;
// Assign the data.
temp -> data = new_data;
last = temp;
// Create the link.
last->next = last;
return last;
}
//insert new node at the beginning of the list
struct Node *insertAtBegin(struct Node *last, int new_data)
{
//if list is empty then add the node by calling insertInEmpty
if (last == nullptr)
return insertInEmpty(last, new_data);
//else create a new node
struct Node *temp = new Node;
//set new data to node
temp -> data = new_data;
temp -> next = last -> next;
last -> next = temp;
return last;
}


您的struct Nodelinkedlist中的内部结构
要么使用linkedlist::Node *last = nullptr;,要么将其移出
您的cpp文件还应该使用linkedlist::作用域来实现Node,否则会出现链接问题。

最新更新