Issue using the pop command for a Queue

I'm getting an error message flag up in the code that I'm writing, the error message states "Cannot initialise a variable of type 'char' with an rvalue of type 'void.'
I understand that char and void are different types of variable, but I don't understand what I'd need to do to rectify the issue, any help or clarification as to where I've gone wrong would be really appreciated, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
void Queue::Print()
{
	// print out all the commands currently in the queue
	for (int i = 0; i < size; i++) //loops the correct number of times
	{
        char nextChar = myQueue.pop(); //taking the first item out of queue
		cout << nextChar;
		myQueue.push(nextChar); //putting the first item back onto the queue
      
	}
	
}
This is because the pop command for a queue doesn't return the value. It simply removes the value at the front of the queue.

You may want to do this instead:
1
2
3
4
5
6
7
8
void Queue::Print() {
    for (std::size_t i = 0; i < myQueue.size(); ++i) {
        char nextChar = myQueue.front(); // get the element at the front
        std::cout << nextChar;
        myQueue.pop(); // remove the element
        myQueue.push(nextChar); // add it to the back of the queue.
    }
}

Note that queues are not really designed for these kinds of operations. Personally, I think it would be better if you went with a std::deque and simply used it like a queue, with combinations of pop_front and push_back. That way it still works like a queue, but you can iterate over it normally.
Last edited on
Thanks for the help, I've been really struggling with Queue functions, I'll take your advice about pop_front and push_back on board though!
Topic archived. No new replies allowed.