c++ - 使用节点将实例添加到链表

标签 c++ linked-list add nodes

如果我想将实例添加到列表中,我无法理解如何调用 main 中的添加函数。

#include "object.h"
class list {

private:
struct Node
{
object objectInfo;
Node *next;

};


int size;
Node *head;


public:

list();
list(const list& otherlist);
~list();

void Add(const Node myObject);

我的主要

int main() {
object myObject;

myObject.setTitle("Object 1");
myObject.setPrice(78.58);
myObject.setISBN("515161611");


cout << myObject << endl;

list myList;

myList.Add(myObject);


return 0;

}

我在cpp中的函数

void list::Add(const Node myObject) {

Node* temp;
temp = new Node; 
temp->objectInfo = myObject.objectInfo;
temp->next = head; 


head = temp;
size++; 

}

我在这条线上遇到了麻烦 myList.Add(myObject); 一直说 void list::Add(const list&)':cannot convert argument 1 from 'object' to 'const list::Node'

也没有重载函数“list::Add”的实例匹配参数列表

最佳答案

您正试图将一个对象类型的对象传递给一个接受节点类型参数的函数。 const Node myObject 应该是 const object myObject。

关于c++ - 使用节点将实例添加到链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36466422/

相关文章:

c++ - 如何对数组 int (*&Test)[10] 进行赋值?

c++ - 通过数组移动指针 - 通过引用或递增传递?

c - 打印链表的不同函数

javascript - 未捕获的类型错误 : sel. 添加不是函数

c++ - 根据 if 语句初始化对象

C++ : How can I solve a first-chance exception caused at an unknown point?

c - 理解 C 中的代码(链表)

c++ - 单链表不起作用 (C++)

web-services - 添加 Netsuite 销售订单项目

loops - 使用 for 循环将元素添加到 F# 中的 Map