[英]Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment
So I have a linked list of 3 books already.所以我已经有3本书的链接列表。 Now I want to add the donated books to the previous linked list.
现在我想将捐赠的书籍添加到以前的链接列表中。 (
donation
is an array of string (book titles) and amount
is the number of books donated). (
donation
是一个字符串数组(书名), amount
是捐赠的书籍数量)。 Here's my code:这是我的代码:
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;
}
}
I got that error on this line tail->next = &donation[i];
我在这条线上遇到了那个错误
tail->next = &donation[i];
. . From what I understand, that line meant that
tail->next
is now pointing to the address of donation[i]
, so I don't know why I'm getting an error?据我了解,该行意味着
tail->next
现在指向donation[i]
的地址,所以我不知道为什么会出错? tail->next
is a pointer so I put ampersand on the donation. tail->next
是一个指针,所以我在捐赠上加上与号。
What is this error and how do I fix this?这是什么错误,我该如何解决?
Agree with the comments above, nodes are not strings, and you can't assign one to the other even when pointers are involved.同意上面的评论,节点不是字符串,即使涉及指针也不能将一个分配给另一个。 I'm guessing that you meant something like this
我猜你的意思是这样的
for (int i=0; i<amount; i+=1)
{
node* n = new node;
n->bookname = donation[i];
tail->next = n;
tail = n;
}
You did this (more or less) correctly in the first half of your function.您在 function 的前半部分(或多或少)正确地做到了这一点。 You only have to do the same here.
你只需要在这里做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.