類對象不工作的Linkedlist模板

我已經創建了這個Dictionary類和一個以這個類為類型的Linkedlist模板。在main()函數中,我試圖創建Dictionary類的對象,并將Dictionary作為Linkedlist模板的類型傳遞。稍后,當我將創建的Dictionary類對象傳遞給LinkedList的insertHead()或initHead()函數并嘗試通過訪問head的數據并調用Dictionary成員函數print()來打印數據時,它不會打印數據(32,A)。insertHead()打印“0-”,initHead()給出分段錯誤。我有辦法解決這個問題嗎?

class Dictionary {
public:
    int index;
    string data;
public:
    Dictionary(int index = 0, string data = ""){
        this->index = index;
        this->data = data;
    }

    Dictionary(string data){
        this->data = data;
    }

    void setIndex(int index){
        this->index = index;
    }

    void setData(string data){
        this->data = data;
    }

    Dictionary operator=(Dictionary const & obj){
        Dictionary dict;
        dict.index = obj.index;
        dict.data = obj.data;
        return dict;
    }

    void print(){
        cout << index << "-" << data << endl;
    }

    friend bool operator== (const Dictionary &d1, const Dictionary &d2);
    friend bool operator!= (const Dictionary &d1, const Dictionary &d2);
};

bool operator== (const Dictionary &d1, const Dictionary &d2){
    return (d1.data == d2.data);
}

bool operator!= (const Dictionary &d1, const Dictionary &d2){
    return !(d1 == d2);
}


int main(){
    Dictionary d(32, "A");
    LinkedList<Dictionary> l;
    l.insertHead(d);
    l.head->data.print();
}

以下是LinkedList模板類的代碼:

#include <iostream>
using namespace std;

template <typename T>
class Node {
public:
    T data;
    Node *next;
    Node *prev;

    Node(){};
};

template <typename T>
class LinkedList {
public:
    Node<T> *head;
    Node<T> *tail;

public:
    LinkedList(){
        head = NULL;
        tail = NULL;
    }

    void initHead(T item){
        head->next = NULL;
        head->prev = NULL;
        head->data = item;
    }

    void insertHead(T item){
        Node<T> *tmp = new Node<T>();
        if (head == nullptr){
            head = tmp;
            head->prev = nullptr;
            head->data = item;
            head->next = nullptr;
            tail = tmp;
        } else {
            tmp->next = head;
            tmp->data = item;
            tmp->prev = nullptr;
            head->prev = tmp;
            head = tmp;
        }
    }

    void insertLast(T item){
        Node<T> *tmp = new Node<T>();
        tmp->data = item;
        if (head == nullptr){
            head = tmp;
            head->prev = nullptr;
            head->next = nullptr;
            tail = tmp;
        } else {
            tmp->prev = tail;
            tail->next = tmp;
            tmp->next = nullptr;
            tail = tmp;
        }
    }

    void insertNext(T item){
        Node<T> *tmp = new Node<T>();
        tmp->next = nullptr;
        tmp->data = item;
        tmp->prev = nullptr;

        Node<T> *iter = head;
        while (iter != nullptr){
            if (iter->next == nullptr){
                iter->next = tmp;
                tmp->prev = iter;
                return;
            }
            iter = iter->next;
        }
    }

    // Returns 0 if Not found. Always add a check
    // for 0 before accessing the tmp->data

    Node<T>* find(T item){
        Node<T> *tmp = head;
        while(tmp && tmp->data != item){
            tmp = tmp->next;
        }
        return tmp;
    }

    bool deleteNode(Node<T>* node){
        if (node == nullptr){
            return false;
        } else if (node == head){
            head = node->next;
            delete node;
            return true;
        } else {
            Node<T> *tmp = head;
            while (tmp){
                if (tmp->next == node){
                    tmp->next = node->next;
                    delete node;
                    return true;
                }
                tmp = tmp->next;
            }
        }
        return false;
    }

    void print(){
        Node<T> *tmp;
        tmp = head;
        while (tmp != nullptr){
            cout << tmp->data << "->";
            tmp = tmp->next;
        }
        cout << endl;
    }
};
? 最佳回答:

在insertHead函數中,我將head->data = item改為下一行

head->data.index = item.index; head->data.data = item.data;

然后它打印了32-A,所以有一個問題,就是=運算符重載。然后我把你的過載改成如下:

void operator=(const Dictionary & obj){ index = obj.index; data = obj.data; }

你字典上的=運算現在正常了。

如果要在注釋中使用原始簽名,則應將以前的重載函數更新為:

 Dictionary& operator=(Dictionary const & obj){
    this->index = obj.index;
    this->data = obj.data;
    return *this;
}
主站蜘蛛池模板: 日韩中文字幕精品免费一区| 亚洲A∨无码一区二区三区| 精品国产一区二区三区在线| 精品国产日韩亚洲一区在线| 中文字幕在线看视频一区二区三区| 中文字幕无码免费久久9一区9| 不卡一区二区在线| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久久久久免费一区二区三区| 一区二区三区在线|欧| 中文字幕亚洲综合精品一区| 精品香蕉一区二区三区| 亚洲熟女一区二区三区| 人妻AV一区二区三区精品 | 色天使亚洲综合一区二区| 色婷婷亚洲一区二区三区| 国产免费一区二区三区在线观看 | 精品一区二区三区四区在线| 精品人妻AV一区二区三区| 国产精品污WWW一区二区三区| 99精品一区二区三区| 色噜噜AV亚洲色一区二区| 91大神在线精品视频一区| 国产精品免费一区二区三区| 日韩精品免费一区二区三区| 日韩综合无码一区二区| 国产情侣一区二区| 国产成人一区二区三中文| 无码AV一区二区三区无码| 国产一区二区在线|播放| 人妻互换精品一区二区| 一区二区免费视频| 久久国产精品视频一区| 亚洲国产精品自在线一区二区 | 亚洲av乱码一区二区三区香蕉| 国产成人精品视频一区二区不卡 | 狠狠综合久久av一区二区| 精品无码国产AV一区二区三区| 男人的天堂av亚洲一区2区| 日韩AV片无码一区二区不卡 | 波多野结衣在线观看一区二区三区|