I am trying to overload the << operator to be an "insert_first operator" for a double linked list. This should be a simple procedure, and I have overloaded operators before, but I am stuck on this one. I am trying to overload it as:
DLList & DLList::operator << (const int value )
{
insert_first(value);
}
My .h file seems fine, and is similar to other ones online:
#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 );
};
and my insert_first function also seems to be working fine:
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;
}
The error I'm receiving is a segfault. Any ideas or comments would be much appreciated.
operator <<when you told the compiler it would return aDLList&.*thismeaningoperator<<has undefined behavior (no return type in a non-void function)head?