Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to include circular_buffer in structure

Tags:

c++

c++11

i am new to cpp. I wish to put "boost::circular_buffer histpos(5);" with 5 spaces to store 5 elements inside a cpp structure as member.

i try this :

typedef struct histposRecorder{
   int32_t track_id;
        boost::circular_buffer<float> histpos(5);

}coRecord;

and i get this error :

error: expected identifier before numeric constant
        boost::circular_buffer<float> histpos(5);
error: expected ‘,’ or ‘...’ before numeric constant

this is the circular_buffer website i am referring to: https://www.boost.org/doc/libs/1_49_0/libs/circular_buffer/doc/circular_buffer.html

please show me how to solve it

The reason i want to put it in structure because i will have a lot different copies of circular_buffer to store the velocities of different objects.

Thanks in advance

like image 653
jetjetboi Avatar asked Mar 04 '23 05:03

jetjetboi


2 Answers

Members can be initialized in a class/struct definition using a brace (or equals) initializer since C++11:

#include <boost/circular_buffer.hpp>

struct coRecord {
  int32_t track_id;
  boost::circular_buffer<float> histpos {5};
};
like image 104
cfillion Avatar answered Mar 15 '23 11:03

cfillion


Try doing the initialization elsewhere:

struct coRecord {
  int32_t track_id;
  boost::circular_buffer< float > buffer;

  coRecord() : buffer(5) {}
};

In you class definition you can only declare member variables, but not initialize them. Instead by providing such a default constructor, you can still do the needed initialization:

coRecord recorder; // automatically reserves 5 places in the buffer
like image 42
Thomas Lang Avatar answered Mar 15 '23 11:03

Thomas Lang