0

I'm currently in the middle of my studies on university and I'm struggling to finish my current project. The assignment was to creat a linked list that will store number bigger than integer and add functions that will do some simple operations like <<,>>,+,-. While I have the whole linked list worked out I'm struggling to think how to overload operator << and >>. I tried few thing, googled it out but no solution actually solves my problem. General idea was to take a number as string then divide it into many 1digit numbers and put it into my list but without working << I can't even start. My list.h

#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED
#include <iostream>
using namespace std;

struct List {
    private:
        struct Node {
            int data;
            Node *next;
            Node(int _data, Node *_next = NULL) {
                data = _data;
                next = _next;
            }
        };
        Node *head, *tail;
        int counter;
        public:
        List();
        ~List();
        friend ostream& operator<<(ostream& wyjscie,const List& L);
        //friend ostream& operator>>(ostream& wejscie,const List& L);
        //customowe funkcje
};

#endif // LISTA_H_INCLUDED

and list.cpp

#include "lista.h"

List::List()
{
    head = NULL;
    tail = NULL;
    counter = 0;
}

List::~List()
{
    while(head != NULL)
    {
        Node *killer = head;
        head = head -> next;
        delete killer;
    }
    tail = NULL;
    counter = 0;
}
ostream& operator<<(ostream& wyjscie,const List& L)
{
    Node *temp;
    for(temp = L.head; temp != 0; temp = temp -> next)
    {
        wyjscie << temp -> data;
        wyjscie<<endl;
    }
    return wyjscie;
}

Problem is that Node is not declared in this scope and when I try to do it without this temporary Node I'm also failing since list is const. Thanks in advance for any tips.

EDIT

First problem solved. I moved to overloading operator>>. I created and insert(string a) function which takes the string, divides it and inserts into the linked list. After doing that I thought that overloading operator>> will be just using that function. I'm not sure why it's not working.

void List::insert(string a)
{
    int n=a.size();
    for(int i=0;i<n;++i)
    {
    Node *nowy=new Node(a[i]);
    if(head==nullptr)
    {
        head=nowy;
        tail=nowy;
    }
    else
    {
        (*tail).next=nowy;
        tail=nowy;
    }
    }
}
istream& operator>>(istream& wejscie,const List& L)
{
    wejscie >> List::insert(string a);
    return wejscie;
}

I also tried something like this:

istream& operator>>(istream& wejscie,const List& L)
{
    string a;
    cin >>a;
    wejscie >> L.insert(a);
    return wejscie;
}

My main function

#include "lista.h"
int main()
{
    List T1;
    List T2;
    T1.insert("54321");
    cout << T1 << endl;
    cin >> T2;
    cout << T2;

}
4
  • 1
    And what's wrong with your current implementation of <<? Looks correct. Please post an minimal reproducible example if you want us to help you. Commented Jan 4, 2020 at 10:42
  • 1
    What is your problem? (compilation error, wrong output, Segmentation Fault, etc.) Please post a Minimal, Complete, and Verifiable example. Your code seem violating The Rule of Three, so List may cause some trouble when it is copied. Commented Jan 4, 2020 at 10:42
  • Sorry, problem is that when I wanted to do the operator<< like this it says that Node from temp wasn't declared. I tried to do it without ,just on L.head but it's const so I can't change it, so I can't go through whole list. Commented Jan 4, 2020 at 10:45
  • Use List::Node * to identify the type of temp, rather than Node *. Declarations in the body ofoperator<<() aren't implicitly referenced to List. Commented Jan 4, 2020 at 11:20

1 Answer 1

1

The function

ostream& operator<<(ostream& wyjscie,const List& L)

is not linked to the List class, so List should be specified to use Node in that.

In other words, you should use List::Node *temp; instead of Node *temp;.

For EDIT part

Why your programs won't work:

istream& operator>>(istream& wejscie,const List& L)
{
    // List::insert isn't telling which list to insert
    // "string a" here is invalid syntax
    wejscie >> List::insert(string a);
    return wejscie;
}
// L is const, so insertion won't be accepted
istream& operator>>(istream& wejscie,const List& L)
{
    string a;
    // cin suddenly appeared from somewhere
    cin >>a;
    // return type of L.insert(a) is void, so it won't suit for operand of >> operator
    wejscie >> L.insert(a);
    return wejscie;
}

(my guess of) What you should do is:

  1. read a string from wejscie
  2. "insert" it to L

An implementation of that:

istream& operator>>(istream& wejscie,List& L)
{
    string a;
    wejscie >> a;
    L.insert(a);
    return wejscie;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that's exactly what was bothering me this whole time. I can't believe I didn't notice it earlier.
If you don't mind I would be greatful for checking my Edit part in this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.