【问题标题】:Problem pointing to a variable in a structure in dynamic memory指向动态内存结构中的变量的问题
【发布时间】:2020-02-10 08:42:59
【问题描述】:

我似乎无法将值输入到我已经声明的结构中。我不确定这是语法错误还是逻辑错误。

我已经尝试过更改语法,但总是以同样的错误告终。

struct Book{

    string title;
    Book* next;
};

Book bookName;
Book author;


Book* add_node(Book* in_root){

    cout <<"Enter Book name \n";
    cin >> bookName.title;

    cout << "Enter author name \n";
    cin >> author;
    author = new Book();
    Book -> Book.next = author;
}

这部分代码遇到错误:

    cout << "Enter author name \n";
    cin >> author;
    author = new Book();
    Book -> Book.next = author;

【问题讨论】:

  • 添加新节点时应该类似于next = new Book(); cin &gt;&gt; next-&gt;title;。
  • @Habitate 是的,谢谢你修复了很多语法错误,但是它说变量 next 没有声明,我不确定我应该准确地给出什么类型。跨度>
  • 只是一些注意事项...... 1)为什么 author 在 Book 结构之外,2)add_node 似乎没有返回任何内容,3)我建议使用智能指针而不是new,因为您需要声明使用 new 创建的 destructor 和 delete 对象。
  • @ignacio 我想将所有内容存储在动态内存中,这就是我将 next 声明为指针的原因。另外,虽然我没有包含它,但我会有另一个函数void delete_list(Book* in_root) 用于删除所有分配的内存。

标签: c++ struct dynamic heap-memory dynamic-memory-allocation


【解决方案1】:

首先,代码中有几个逻辑错误。

  • 除非我误解了它们的目的,否则绝对没有必要将两本书命名为 bookName 和 author。
  • Book -&gt; 和 Book.next 是无效逻辑,因为您告诉它对数据类型 Book 进行操作,而不是 Book 类型的对象。

您可能想要的代码应该如下所示:

#include <iostream>
#include <string>

using namespace std;

struct Book{
    string title;
    string author_name; // You potentially wanted this?

    Book* next;
};

// This function assumes that `current_node->next` is `nullptr`
// The reasons for this is that making it handle such cases might be too difficult for you yet.
Book* add_node(Book* current_book){
    if(current_book == nullptr){
        cout << "Cannot link a new book to an non-existant book!\n";
        return nullptr;
    }

    Book* new_book = new Book();

    cout <<"Enter the book name\n";
    cin >> new_book->title;

    cout << "Enter the author name\n";
    cin >> new_book->author_name;

    new_book->next = nullptr;

    current_book->next = new_book;
    return new_book;
}

int main(){
    Book* book = new Book();
    book->next = nullptr;

    cout <<"Enter the name of the first book\n";
    cin >> book->title;

    cout << "Enter the name of the first book's author\n";
    cin >> book->author_name;

    add_node(add_node(book));

    return 0;
}

current_book-&gt;next != nullptr 时我没有使函数处理案例的原因是因为它需要使用指向指针的指针。 如果你对它感兴趣,这里是:

Book* add_node_v2(Book* current_book){
    if(current_book == nullptr){
        cout << "Cannot link a new book to an non-existant book!\n";
        return nullptr;
    }

    Book* new_book = new Book();

    cout <<"Enter the book name\n";
    cin >> new_book->title;

    cout << "Enter the author name\n";
    cin >> new_book->author_name;

    new_book->next = nullptr;

    // Move to the last book in the chain
    Book** ptr_to_next = &current_book->next;
    while(*ptr_to_next != nullptr){
        ptr_to_next = &(*ptr_to_next)->next; 
    }

    *ptr_to_next = new_book;
    return new_book;
}

请记住,您最终必须delete 链中的所有书籍。

【讨论】:

    猜你喜欢
    • 2015-05-17
    • 2012-07-16
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2021-12-26
    相关资源
    最近更新 更多