Home > database >  Passing object to << overload in alters data in object in C
Passing object to << overload in alters data in object in C

Time:10-23

I'm trying to write a list class in C , and I want to overload the << operator. However, when I pass the list object to the operator function, changes the values within the object. Other questions on StackOverflow have not helped resolve this. Superfluous code has been removed from the following:

LinkedList.h:

#include "Node.h"
#include <iostream>

class LinkedList {
public:
    Node* head;
    Node* tail;
    int size;

    LinkedList();
    void push(int data);

    friend std::ostream& operator<<(std::ostream& os, const LinkedList& list);
};

This is LinkedList.cpp:

#include "LinkedList.h"

LinkedList::LinkedList() {
    head = nullptr;
    tail = nullptr;
    size = 0;
}

void LinkedList::push(int data) {
    Node newNode = Node(data);
    if (size == 0) {
        head = &newNode;
        tail = &newNode;
    } else {
        tail->next = &newNode;
        newNode.prev = tail;
        tail->next = &newNode;
    }
    size  ;
}


std::ostream& operator<<(std::ostream& os, const LinkedList& list) {
    os << "[";
    os << list.head->data;
    os << "]";
    return os;
}

(At the moment I just want it to output the data in the first element in the list)

Here is the driver code:

#include "LinkedList.h"

int main() {
    LinkedList lis = LinkedList();
    lis.push(5);
    std::cout << lis;
}

Please be gentle, I'm new to C .

Edits: Sorry, here is Node.h:

#pragma once
class Node {
public:
    int data;
    Node* next;
    Node* prev;

    Node(int data);
};

and Node.cpp

#include "Node.h"

Node::Node(int initdata) {
    data = initdata;
    next = nullptr;
    prev = nullptr;
}

I have also added the rest of LinkedList.cpp. The output I had expected from the above code is [5], since I am pushing 5 onto the list and then outputting it (or, at least, trying to).

CodePudding user response:

You need to allocate new nodes with the new operator.

This is your corrected code:

#include <iostream>

class Node {
public:
  int data;
  Node *next;
  Node *prev;

  Node(int data);
};  

Node::Node(int initdata) {
  data = initdata;
  next = nullptr;
  prev = nullptr;
}

class LinkedList {
public:
  Node *head;                       // <<<<<<<<<<< change here
  Node *tail;                       // <<<<<<<<<<< change here
  int size;

  LinkedList();
  void push(int data);

  friend std::ostream& operator<<(std::ostream& os, const LinkedList& list);
};

LinkedList::LinkedList() {
  head = nullptr;
  tail = nullptr;
  size = 0;
}

void LinkedList::push(int data) {
  Node *newNode = new Node(data);   // <<<<<<<<<<< change here
  if (size == 0) {
    head = newNode;                 // <<<<<<<<<<< change here
    tail = newNode;                 // <<<<<<<<<<< change here
  }
  else {
    tail->next = newNode;
    newNode->prev = tail;
    tail->next = newNode;
  }
  size  ;
}

std::ostream& operator<<(std::ostream& os, const LinkedList& list) {
  os << "[";
  os << list.head->data;
  os << "]";
  return os;
}

int main() {
  LinkedList lis = LinkedList();
  lis.push(5);
  std::cout << lis;
}
  • Related