因此,我已经有3本图书的链接列表。现在,我想将捐赠的书籍添加到上一个链接列表中。 (donation
是字符串(书名)的数组,而amount
是捐赠的书数)。这是我的代码:
void newbrowse(int amount, string donation[])
{
head= new node;
second=new node;
tail= new node;
head->bookname = "Book1";
head->next = second;
second->bookname = "Book2";
second->next = tail;
tail->bookname = "Book3";
for (int i=0; i<amount; i+=1)
{
tail->next = &donation[i];
tail = donation[i];
}
display = head;
cout<<"Total books:"<<endl;
for (int j=1; j<=(amount+3); j+=1)
{
cout<<display->bookname<<endl;
display = display->next;
}
}
我在此行tail->next = &donation[i];
上遇到了该错误。据我了解,该行表示tail->next
现在指向donation[i]
的地址,所以我不知道为什么会出错? tail->next
是一个指针,所以我在捐款上加上了&符。
这是什么错误,我该如何解决?
答案 0 :(得分:0)
同意以上注释,节点不是字符串,即使涉及指针,也不能将一个分配给另一个。我猜你的意思是这样的
for (int i=0; i<amount; i+=1)
{
node* n = new node;
n->bookname = donation[i];
tail->next = n;
tail = n;
}
您在函数的前半部分正确地(或多或少)完成了此操作。您只需要在这里做同样的事情。