Question

I'm trying to implement a circular buffer for an assignment. To save time, I want to use a deque inside my reorder buffer class. Here's my first attempt at writing a class that contains a deque.

#ifndef ROB_H_
#define ROB_H_

#include <deque>
#include <cstdio>

using namespace std;

class ReorderBuffer{
    public:
        ReorderBuffer (int size);
        void doStuff();

        std::deque<int> buffer;
};    

ReorderBuffer::ReorderBuffer (int size){
    std::deque<int> buffer(size);
}

void ReorderBuffer::doStuff(){

    std::deque<int> buffer(4);

    buffer.push_back(5);
    buffer.push_front(2);
    buffer.push_back(3);
    buffer.push_back(4);
    printf("%d %d\n",buffer.at(0),buffer.pop_front());

}


#endif

In main, I make a reorder buffer with size 4 and call doStuff(). When I try to compile, it says invalid use of void expression. I have narrowed the error down to my call of buffer.pop_front(). Why is it complaining, and what is the best way to put a deque in my class? Thanks!

Was it helpful?

Solution

std::deque::pop_front returns void. You cannot print this with the result of this function. Use at() to get values and then use pop_front or pop_back to simply remove the front or back element as needed, but note that they do not return anything.

http://en.cppreference.com/w/cpp/container/deque/pop_front

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top