简体   繁体   English

错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char>

[英]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.

相关问题 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment 为什么我收到错误:无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char*&#39; 赋值? - Why am I getting error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in assignment? 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM