I wanted to implement a fixed size queue, I could have easily done with a struct having a queue as one of the data members and a member function that takes care of the push part of the queue, but rather wanted to try it out by inheriting queue like this.
template<typename T>
struct f_queue : public queue<T>{
int n;
T prev;
f_queue(int n_):
n(n_){}
void push(T data){
if(this->size() < this->n){
this->push(data);
}else{
prev = this->front();
this->pop();
this->push(data);
}
}
};
This compiles just fine but for some reason it gives a segmentation fault and the gdb says
Program received signal SIGSEGV, Segmentation fault. 0x0000555555555862 in std::_Deque_iterator<int, int&, int*>::_S_buffer_size() ()
Not really sure what that is supposed to mean? When tried doing a backtrace the output comes out to be
#9913 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9914 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9915 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9916 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9917 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9918 0x0000555555554ec6 in f_queue<int>::push(int) ()
#9919 0x0000555555554ec6 in f_queue<int>::push(int) ()
doesn't stop goes infinitely. Need help with this.Thanks in advance.
this->push(data);. I assume, you actually wantqueue<T>::push(data);to refer topush()of base class.