2

I am using boost library in MAC (xcode). I have two questions about boost::circular_buffer.

1 - I am getting syntax error when declaring the circular_buffer

boost::circular_buffer<int> cb(10);

Expected parameter decelerator
Expected ')'

2 - Second question is when I add element into boost::circular_buffer using push_back, how to extract / get the element from circular_buffer, pop_front does not give the element.

Gordon Brandly
  • 352
  • 4
  • 15
Ibrar Ahmed
  • 1,039
  • 1
  • 13
  • 25
  • Did you read the documentation? http://www.boost.org/doc/libs/1_55_0/doc/html/circular_buffer/example.html `front()` and `pop_front()` or `back()` and `pop_back()`. – Chad Feb 13 '14 at 16:56
  • Yes, this example does not work, getting syntax error at (10) on line boost::circular_buffer cb(10); Secondly I dont want to indexed the circular queue like cb[0], cb[1]. `pop_front`, `pop_back` does not give the element (may be I am wrong here, but example is not using these function to get the element) – Ibrar Ahmed Feb 13 '14 at 17:05
  • Ditch the compiler that uses the word `decelerator` in an error message. It's only going to slow you down :/ – sehe Feb 13 '14 at 22:36

1 Answers1

4

boost::circular_buffer<T>::front() gives you a reference to the element at the "front", while boost::circular_buffer<T>::pop_front() will remove that element.

boost::circular_buffer<T>::back() gives you a reference to the element at the back, while boost::circular_buffer<T>::pop_back() removes that element.

It appears your syntax error is resulting from the most vexing parse. Try instead:

boost::circular_buffer<int> cb;
cb.set_capacity(10); 

Or more succinctly:

boost::circular_buffer<int> cb((10));
Chad
  • 18,706
  • 4
  • 46
  • 63