简体   繁体   中英

Implementing Circular Buffer in C++ using deque

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!

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

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