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
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};
};
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With