【问题标题】:.exe has stopped working code blocks cpp.exe 已停止工作代码块 cpp
【发布时间】:2016-01-20 22:07:15
【问题描述】:

`你好 我刚刚在代码块中编写了这段代码,在构建和运行之后,它说程序已经停止工作。我找不到我做错了什么。我不知道问题是否与我的代码有关,或者与我正在使用的编译器或其他问题有关。 请帮忙

库.h:

#ifndef LIBRARY_H
#define LIBRARY_H
#include <iostream>
#include <string>
using namespace std;
class Library
{
public:
    struct book
    {

        string tittle;
        int number;
        struct book* next;
    }* head, *tail, *ptr;

    Library();
    ~Library();
    book* searchName(book *, string);
    void addNode(book *);
    book *initNode(string s, int i);
    void displayNode(book *ptr) const;
    void displayList(book *ptr) const;
protected:
};

#endif

库.cpp

#include "Library.h"

Library::Library() :
        head(NULL), tail(NULL)
{
}

Library::~Library()
{
    book *current, *temp;

    current = head;

    temp = head;
    while (current != NULL)
    {
        current = current->next;
        delete temp;
        temp = current;
    }
}

Library::book * Library::searchName(Library::book* ptr, string name)
{
    while (name != ptr->tittle)
    {
        ptr = ptr->next;
        if (ptr == NULL)
            break;
    }
    return ptr;
}

void Library::addNode(book *newNode)
{
    if (head == NULL)
    {
        head = newNode;
        head = newNode;
    }
    tail->next = newNode;
    newNode->next = NULL;
    tail = newNode;
}

Library::book *Library::initNode(string s, int i)
{
    book *ptr = new book;
    if (ptr == NULL)
        return static_cast<book *>(NULL);
    else
    {
        ptr->tittle = s;
        ptr->number = i;
        return ptr;
    }
}

void Library::displayNode(book *ptr) const
{

    cout << ptr->number << ": " << ptr->tittle << endl;
}

void Library::displayList(book *ptr) const
{
    if (!ptr)
        cout << "Nothing to display" << endl;
    while (ptr)
    {
        displayNode(ptr);
        ptr = ptr->next;
    }
}

main.cpp

#include "Library.h"
#include <iostream>
using namespace std;

int main()
{
    Library a;
    Library::book *ptrr;
    ptrr = a.initNode("s1", 1);
    a.addNode(ptrr);
    ptrr = a.initNode("s2", 2);
    a.addNode(ptrr);
    a.displayList(a.head);
}

【问题讨论】:

  • 缩进你的代码,然后使用调试器......你会发现错误在哪里。
  • Library::book *Library::initNode(string s, int i) 应该是 book 构造函数。 Library 不必知道如何构建一本书。那是book's 的工作。
  • void displayList(book *ptr) const 不得不将一本书传递给displayList 似乎有点奇怪。 displayList 已经知道它的列表。当前的实现将允许用户调用a.displayList(b.head);,以获取b 的列表,这很愚蠢。另一方面,static void displayList(book *ptr) 是有道理的。
  • displayNode 类似,但应该是book 的成员。 Library 没有理由知道如何打印 book

标签: c++ pointers null codeblocks singly-linked-list


【解决方案1】:

当您从main 调用第一个a.addNode(ptrr) 时,它会调用tail-&gt;next = newNode;(在Library::addNode 内),但是由于tailNULL(尚未分配),它会崩溃......

现在,代码中可能还有其他问题,但这很可能是第一个导致程序停止工作的问题...

请注意,您的代码中的某些内容可能会被简化。

喜欢:

Library::book *Library::initNode(string s, int i)
{
    book *ptr = new book;
    if (ptr == NULL)
        return static_cast<book *>(NULL);
    else
    {
        ptr->tittle = s;
        ptr->number = i;
        return ptr;
    }
}

可能只是:

Library::book *Library::initNode(string s, int i)
{
    book *ptr = new book;
    if (ptr != NULL)
    {
        ptr->tittle = s;
        ptr->number = i;
    }
    return ptr;
}

还有:

void Library::addNode(book *newNode)
{
    if (head == NULL)
    {
        head = newNode;
        head = newNode;
    }
    tail->next = newNode;
    newNode->next = NULL;
    tail = newNode;
}

应该是:

void Library::addNode(book *newNode)
{
    if ( newNode != NULL ) // just in case
    {
        if (head == NULL)
        {
            head = newNode; // one is enough
        }
        if ( tail != NULL ) // to fix your crash
            tail->next = newNode;
        newNode->next = NULL;
        tail = newNode;
    }
}

另外,看user4581301的评论,析构代码甚至没有编译......

【讨论】:

  • @user4581301:这是什么意思?
  • libri *current, *temp; 不是那种可以编译的东西。 book *current, *temp;,另一方面...
  • @user4581301:是的,但是,实际上,我没有解决它.....我根本没有尝试编译代码......;-)
  • 我的意思是 book 而不是 libri ..问题是我写的是 head = newNode ,而不是 tail=newNode ..我现在工作得很好
  • @D.K 您还需要将 tail-&gt;next = newNode; newNode-&gt;next = NULL; tail = newNode; 移动到 else 中。如果没有 else 工作,但它是一个令人费解的混乱或指向自身的东西。
猜你喜欢
  • 1970-01-01
  • 2014-01-20
  • 2013-08-09
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多