Linked list, operator >> and << overloading.

For some reason when I overload operators << and >> in my linked, that I will be using for my inventory system, I am unable to access the private members of my list within the overloaded operator? I can't even access the linkedlist that was passed in by reference's members. Maybe I am doing something wrong with the overloading?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <string>
#include <iostream>
#include "save.h"
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<typename STORAGE> class linkedlist
{
private:
	struct component {
		component* next;
		STORAGE info;
	};
	component* head;
	component* tail;
	std::string file;
	short unsigned int items_amount;
	bool save;

public:
	linkedlist();
	linkedlist(std::string txt);
	~linkedlist();
	STORAGE use(unsigned int index);
	STORAGE ret_info(unsigned int index);
	void insertItem(unsigned int index, STORAGE item);
	void insert_mult_items(STORAGE arg_info[], int size, int start);

	void pop_front();
	void pop_end();
	void erase(unsigned int start, unsigned int end);
	void removeItem(unsigned int index);

	void addItem_end(STORAGE item);
	void addItem_front(STORAGE item);
	void addItem_end(STORAGE arg_info[], int size);
	void addItem_front(STORAGE arg_info[], int size);

	void save_items(std::string txt);

	int retItems_amount() const { return items_amount; }
	bool is_empty() const;

	void remove(STORAGE type);
	void swap(linkedlist<STORAGE>& swap);
	void swap(linkedlist<STORAGE>* swap);
	void resize(unsigned int new_size);
	void clearAll();
	void sort();
	linkedlist operator+(const linkedlist& list) {
		linkedlist copy; 
		copy.head = this->head;
		copy.items_amount = this->items_amount + list.items_amount;
		copy.save = this->save;
		copy.file = this->file;
		this->tail->next = list.head;
		this->tail = list.tail;
		copy.tail = this->tail;
		return copy;
	}
	friend std::ostream& operator<<(std::ostream output, linkedlist<STORAGE>& list) {
		
		return output;
	}
	friend std::istream& operator>>(std::istream& input, linkedlist<STORAGE>& list) {
		
		return input;
	}

Of course there is more code to this but it is around 400lines and growing so yeah?
Works fine when I try. What error message did you get?
I'm not getting an error it's just that something is weird within the overloaded operators>> and <<. Like when I want to access one of the private members within my linked list class I am unable to. I can't use a this-> pointer either to get private members. Nor can i use the referenced list to get the private members. Instead I only get the public function not the private variables when I try to access it.
Perhaps it has to do with operator << taking a std::ostream by value instead of reference. The linkedlist parameter should be const as well.

No, you won't be able to use the this pointer. It's not a member function, but it is a friend so you will be able to access private members (although you shouldn't need it at all if the class has a reasonable public interface.)
Maybe, although I'd like to be able to display all of the contents via the overloaded << operator. And well I can't do that because I can't acess the head pointer. Although, I could have a function that return the head pointer and then iterate through that although what do you think? Is that a good or bad idea?
Last edited on
@Cire Man I'm so dumb. I didn't even notice I was taking the istream and ostream by value rather than by reference. I thought I did that in the first place. I thought you missed something...Facepalm... Anyways scrap what I said earlier. Thanks cire you always got my back.
Last edited on
Topic archived. No new replies allowed.