【问题标题】:C++ Unable to Print the Data in Pointers of a Linked List after I've Traversed the List without Adding a Node在未添加节点的情况下遍历列表后,C++ 无法打印链接列表指针中的数据
【发布时间】:2015-06-05 23:13:57
【问题描述】:

我正在处理一个双向链表。它由类组成,以当前节点为中心(而不是列表开头或结尾的节点)。现在我的打印函数会抛出一个错误,但前提是我已经遍历了列表。我的打印功能只是打印当前节点中的数据(如果它不为空)。这是我的打印功能:(在底部对我的文件层次结构和代码进行了更详细的描述)

void queue::print(){
    if (current){
        std::cout << std::endl << std::endl << "+++++++++++++++++++ Webpage +++++++++++++++++++" << std::endl
            << "URL: " << current->data.getURL() << std::endl
            << "-----------------------------------------------" << std::endl 
            << "Title: " << current->data.getTitle() << std::endl
            << "-----------------------------------------------" << std::endl
            << "Content: " << current->data.getContent() << std::endl
            << "+++++++++++++++++++++++++++++++++++++++++++++++" << std::endl << std::endl;
    }
    else{
        std::cout << std::endl << "Your not on a page. Please navigate to a page first." << std::endl;
    }

现在,如果我用两个数据节点填充列表,并执行我的打印功能,它会很好地打印节点中的数据。但是,如果我使用 goBack() 函数遍历到上一个节点:

void queue::goBack(){
    if (!current->previous){
        std::cout << "No previous page to go to!" << std::endl;
    }
    else{
        temp2 = current;
        current = current->previous;
    }
}

这将执行得很好,但是当我尝试在节点中打印数据时(使用相同的打印功能)我收到此错误:

Visual Studio 会打开一个没有扩展类型的文件,其中包含 C 代码,称为 xstring,其中有一个指向第 1754 行的中断箭头。

现在让我更详细地解释一下我的代码。我有五个文件:webQueue.h、webQueue.cpp、webPage.h、webPage.cpp 和 main.cpp。我列出的所有函数都在我的 webQueue.cpp 文件中。

这里是 webQueue.h:

#include "webPage.h"
class queue{
public:
    queue();
    void newPage(std::string u, std::string t, std::string c);
    void goForward();
    void goBack();
    void print();
private:
    struct Node{
        webPage data;
        Node* next;
        Node* previous;
    };
    Node* temp;
    Node* current;
    Node* temp2;
};

这里是 webPage.h:

#include <string>
class webPage{
public:
    webPage();
    webPage(std::string u, std::string t, std::string c);
    std::string getURL();
    void setURL(std::string u);
    std::string getTitle();
    void setTitle(std::string t);
    std::string getContent();
    void setContent(std::string c);
private:
    std::string URL;
    std::string title;
    std::string content;
};

我的 webQueue.cpp 文件包括:

#include <iostream>
#include "webQueue.h"

我的 webPage.cpp 文件只包含 webPage.h,而我的 main.cpp 文件(包含我的执行函数)包括:

#include <iostream>
#include <string>
#include "webQueue.h"

虽然这些关系看起来有些复杂,但它们都应该是有效的。 cpp 文件以完全相同的名称链接到它们的头文件(前提是头文件存在),main.cpp 链接到 webQueue.h,webQueue.h 链接到 webPage.h。我看不出我的代码做错了什么——尽管这可能只是因为我很难理解指针是如何工作的。我想错误出现在我的 print()、goBack() 和 goForward() 函数的代码中(尽管在修复 goBack() 函数之前我无法测试我的 goForward() 函数)但我不能告诉你出了什么问题。

你们可以提供的任何帮助将不胜感激,因为我很难过。这是所有文件的 Dropbox 链接,您可以自己测试这个程序,看看我是否还有其他功能出错:https://www.dropbox.com/s/yekrz6dln1v9npk/webQueue.zip?dl=0

【问题讨论】:

    标签: c++ linkedin nodes


    【解决方案1】:

    您的queue::newPage 函数在添加新页面时不会初始化previous 节点,这意味着第一个节点的previous 指向随机垃圾。您需要将其设置为nullptr(最好您的Node 结构应该有一个自动执行此操作的构造函数)。

    【讨论】:

    • 我不敢相信我错过了。这是我之前犯过两次的错误!但这并不能解决问题。 ಠ~ಠ
    • 另外,如何在默认构造函数中初始化类中的结构内的节点?
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多