重载<< C ++中双向链表中的运算符

时间:2015-12-17 20:33:31

标签: c++

我正在尝试将<< operator重载为&#34; insert_first运算符&#34; 以获取双链表。这应该是一个简单的过程,我之前已经重载了运算符,但我坚持这个。我试图将其重载为:

DLList & DLList::operator << (const int value ) 
{
    insert_first(value);
}

我的.h文件似乎很好,与其他在线文件类似:

#ifndef DLLIST_H
#define DLLISH_H

#include <stdio.h>
#include <iostream>

class ListNode {
public:
    int value; 

    ListNode * next;
    ListNode * prev;

    static int node_count;

    ListNode() {
        node_count++;
    }

    ~ListNode() {
        node_count--;
    }

    static void print_node_count() {
        std::cout << "Node Count: " << node_count << std::endl;
    }
};

class DLList {
private:
    ListNode * head;
public:
    DLList();
    ~DLList();

    void print_list();
    void print_list( std::string list_name );
    void insert_first( int value );

    bool remove( int value );
    void concat_list( DLList * list_to_add );

    DLList & operator << (const int value );
};

我的insert_first函数似乎也运行良好:

void DLList::insert_first( int value ) 
{
    ListNode * n = new ListNode();

    n->value = value;
    n->next = head->next;
    n->prev = head;

    head->next = n;
    n->next->prev = n;    
}

我收到的错误是段错误。任何想法或意见将不胜感激。

1 个答案:

答案 0 :(得分:1)

好的,正如评论建议的那样,我需要从运营商那里返回一些内容,如

DLList & DLList::operator << (const int value ) 
{
insert_first(value);
return *this; 
}
相关问题