简体   繁体   中英

Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment

So I have a linked list of 3 books already. 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). 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]; . 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 is a pointer so I put ampersand on the donation.

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. You only have to do the same here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM