简体   繁体   中英

Overloading operator << in linked list

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;

}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM