c++ - 在 Windows 上工作,但在 Ubuntu 上出现段错误

标签 c++

所以我在这里的不同线程中看到了此类问题,但找不到这种情况所需的答案...

就像我在标题中提到的那样,我的代码在 Visual Studio 2013 上运行良好,没有内存泄漏或任何类似的问题,但是当通过 Ubuntu 的平台运行时,它运行到调用复制构造函数的位置,然后在将第二个节点设置为新节点,我尝试将构造函数标题中“const”的位置切换到函数之后或类型名称之后,但它根本不起作用,我尝试更改函数,但什么也没做,我尝试以不同的方式调用复制构造函数,结果也一样,我也尝试向我的实践老师展示这个,你可以猜到结果,因为我是还在写这篇文章:P

那我错过了什么?

下面的代码表示一个循环链表,每个节点有 2 种数据类型: .hpp

#include <iostream>
#include <string>

using namespace std;

class MyLinkedList{
private:
    class Node{
    public:
        Node* next;
        Node* prev;
        string key;
        double data;

        Node(const string key, const double data)
        {
            this->next = this->prev = NULL;
            this->key = key;
            this->data = data;
        }

        Node(const Node* node)
        {
            this->next = this->prev = NULL;
            this->key = node->key;
            this->data = node->data;
        }
    };//END OF NODE

    Node* head;
    Node* tail;

public:
    MyLinkedList();
    void add(const string& key, const double& data);
    MyLinkedList(const MyLinkedList& MLL);
    ~MyLinkedList();
    bool empty();
    void printList();
    MyLinkedList& operator=(const MyLinkedList&);
    int remove(const string&);
    bool isInList(const string, double&);
    double sumList();
};

.cpp(部分):

#include "MyLinkedList.hpp"

MyLinkedList::MyLinkedList(){
    head = tail = NULL;
}

void MyLinkedList::add(const string& key, const double& data){
    if (empty()){
        head = new Node(key, data);
        tail = head->next = head->prev = head;
        tail->next = tail->prev = head;
        return;
    }

    tail->next = new Node(key, data);//**(Segmentation fault HERE)**
    tail->next->prev = tail;
    tail = tail->next;
    tail->next = head;

    head->prev = tail;
}


MyLinkedList::MyLinkedList(const MyLinkedList& MLL){
    if (MLL.head == NULL){
        head = tail = NULL;
        return;
    }

    Node* N1 = MLL.head;
    do{
        add(N1->key, N1->data);//<--falls on the second time
        N1 = N1->next;
    } while (N1 != MLL.head);
}

bool MyLinkedList::empty(){
return head==NULL;
}

int main(){
    MyLinkedList A;
    A.add("key1", 1);
    A.printList();
    A.add("key4", 2);
    A.add("key3", 3);
    A.add("key4", 4);
    A.printList();
    cout << "sum: " << A.sumList() << endl;
    MyLinkedList A2(A);// <--Segmentation fault within.
    A2.printList();
}

最佳答案

您不像在默认构造函数中那样在复制构造函数中初始化指针,因此当您调用 add 时它们是悬空的。做

MyLinkedList::MyLinkedList(const MyLinkedList& MLL) {
  head = tail = NULL;

  // Rest as before

一些旁注:由于您使用的是 MSVC 2013,它具有(有限的)C++11 支持,您还可以编写

// nullptr is the typesafe C++11 way to write NULL
Node* head = nullptr;
Node* tail = nullptr;

在类里面,再也不用担心了。另一个风格说明是,在 ctor init 列表中而不是在构造函数体内初始化数据成员可以说是更好的风格:

MyLinkedList::MyLinkedList(const MyLinkedList& MLL)
 : head(nullptr),
   tail(nullptr) {
  ...

因为这也适用于不可默认构造的成员,例如引用。

关于c++ - 在 Windows 上工作,但在 Ubuntu 上出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27580038/

相关文章:

c++ - 什么时候调用 CMFCListCtrl::OnGetCellBkColor 函数?

c++ - 在 Windows 7 中交叉编译 C 和 C++ 应用程序,在 linux 下使用 MinGW

C++ 模板函数获取错误的默认值

c++ - 事务中的sqlite3_step(stmt)失败,错误5,而没有调用我的忙处理程序

c++ - 从游戏中的可移动空间中删除矩形

c++ - C++中的低级蓝牙编程

c++ - 将字符串 vector 连接到 std::ostream(如 boost::join)

c++ - FreeType - Texture Atlas - 为什么我的文本呈现为四边形?

c++ - 强制所有类在多级继承层次结构中实现/覆盖 'pure virtual' 方法

c++ - 我如何创建一个仅包含 C 代码包装的 C++ 代码的 .so 文件 - OpenCV 相关?