LinkedQueue

Okay this is a homework question, but my problem is that I really dont know what to do

basically I need to overload the ostream operator, the problem being is that this is my header for the queue class.

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
#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H

#include <iostream>
#include "RuntimeException.h"
#include "LinkedList.h"

class LinkedQueue {
private:
   LinkedList ll;

public:
   // user-defined exceptions
   class QueueEmptyException : public RuntimeException {
   public:
     QueueEmptyException() : RuntimeException("Access to an empty queue") {}
   };

   LinkedQueue() : ll() { } // constructor
   ~LinkedQueue() { ll.~LinkedList(); } // destructor
   LinkedQueue(const LinkedQueue& queue) {...} // copy constructor
   LinkedQueue& operator=(const LinkedQueue& queue); // assignment operator
   
   // query function
   int size() const { return ll.size(); }
   bool isEmpty() const {  return ll.isEmpty();  }
   
   // accessor function
   char first() const throw(QueueEmptyException);
   
   // update functions
   void enqueue(char elem) { ll.insertLast(elem); }
   char dequeue() throw(QueueEmptyException);
   
   friend ostream & operator<<(ostream& out, const LinkedQueue& queue); // overload <<
};

#endif 


and here is my closest attempt

1
2
3
4
5
6
7
8
9
ostream& operator<<(ostream& out, const LinkedQueue& queue)
{
  ListNode *iter = new ListNode(queue.first());
  while(iter != NULL) {
	  out << iter->getElem() << " ";
	  iter = iter->getNext();
  }
  return out;
}


This is my closest because it will at least print out the first element but that is it. I already did a linkedlist where I set an listnode to point to the head of the list, and then while cycling through it I would just getElem(), so I'm trying that here, however my problem this time is that LinkedQueue doesnt have a head, and I cant use the ll.getHead() because LinkedList ll is private.

So could someone point me in the right direction on how to do this?
I cant use the ll.getHead() because LinkedList ll is private.
To avoid this problem is exactly why you made operator << a friend function.

This cannot be done without access to head of ll. If you really can't do it, you made a mistake while giving access rights to members of LinkedList (getHead should be public).
Topic archived. No new replies allowed.