rlogiacco / CircularBuffer

Arduino circular buffer library
GNU Lesser General Public License v3.0
312 stars 85 forks source link

How to use these in an array? #83

Closed scspijker closed 10 months ago

scspijker commented 11 months ago

I would like to store a number of buffers, and access them anonymously through the array. However, I cannot find a way to do this...

What I expected to be able to do:

CircularBuffer<int, 10> keyboardBuffers[] = {
  CircularBuffer<int,10>(),
  CircularBuffer<int,10>(),
  CircularBuffer<int,10>()
}

keyboardBuffers[0][0];

But this results in:

error: use of deleted function 'CircularBuffer<T, S, IT>::CircularBuffer(CircularBuffer<T, S, IT>&&) [with T = int; unsigned int S = 10; IT = unsigned char]'
 }
 ^
libraries/CircularBuffer/CircularBuffer.h:59:2: note: declared here
  CircularBuffer(CircularBuffer&&) = delete;
  ^~~~~~~~~~~~~~

So I tried using pointers:

CircularBuffer<int, 10>* keyboardBuffers[] = {
  &CircularBuffer<int,10>(),
  &CircularBuffer<int,10>(),
  &CircularBuffer<int,10>()
};

keyboardBuffers[0]->size();

But this results in

error: 'keyboardBuffers' does not name a type
 keyboardBuffers[0]->size();
 ^~~~~~~~~~~~~~~

Can anybody tell me how to use this class in an array in such a way I can access the array subset operator [<index>]? Thanks!

rlogiacco commented 11 months ago

Can you please try the following code? My compiler doesn't complain at all...

#include <CircularBuffer.h>

CircularBuffer<int, 10> *buffers[] = {
  &CircularBuffer<int, 10>(), &CircularBuffer<int, 10>(), &CircularBuffer<int, 10>()
};

void setup() { }

void loop() {
  buffers[0]->push(3);
  buffers[1]->push(3);
  buffers[2]->push(3);
  buffers[2]->size();
}
github-actions[bot] commented 10 months ago

This issue is being marked as stale as it has not been updated in the past 15 days and it will be closed in a week unless it receives the requested attention.

github-actions[bot] commented 10 months ago

Issue is being closed due to lack of interaction