0

I'm writing an application for Arduino (more precisely, Teensy). While compiling the following code (last pasted line):

void executeActions(std::shared_ptr<unsigned char[]> actionData, const unsigned short actionBytes)
{
  unsigned short modifier = 0;
  Keyboard.set_modifier(modifier);

  int i = 0;
  while (i < actionBytes)
  {
    unsigned char action = actionData[i];

I'm getting the following error:

no match for 'operator[]' (operand types are 'std::shared_ptr<unsigned char []>' and 'int')

For reference, mentioned actionData is initialized in the following way (then passed a couple of times):

this->actionData = std::make_shared<unsigned char[]>(new unsigned char[this->actionBytes]);

What am I doing wrong?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • 2
    The `shared_ptr` doesn't have a `[]`. You need to apply `[]` to the array referenced by the `sthared_ptr`. [This help any](https://stackoverflow.com/questions/30780262/accessing-array-of-shared-ptr)? – user4581301 Mar 24 '22 at 20:46
  • `shared_ptr` does have `operator[]` but only in C++17 and later. – Remy Lebeau Mar 24 '22 at 21:56

1 Answers1

1

In C++17 and later, std::shared_ptr has an operator[] (only when T is an array type). This operator does not exist in C++11..14, which is apparently what you are compiling for, or at a cursory glance, Arduino does not fully support C++17.

Also:

std::make_shared<unsigned char[]>(new unsigned char[this->actionBytes])

should be:

std::make_shared<unsigned char[]>(actionBytes)

But that is only available in C++20 and later.

In this situation, I would suggest using std::vector<unsigned char> instead of unsigned char[] (you should not be using new manually anyway), eg:

actionData = std::make_shared<std::vector<unsigned char>>(actionBytes);
void executeActions(std::shared_ptr<std::vector<unsigned char>> actionData)
{
  unsigned short modifier = 0;
  Keyboard.set_modifier(modifier);

  for(size_t i = 0; i < actionData->size(); ++i)
  {
    unsigned char action = (*actionData)[i];
    ...
  }

  /* simpler:
  for (auto action : *actionData)
  {
    ...
  }
  */

  ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770