0

I have a constructor which looks like this:

Preset::Preset(char* presetName, MIDICommand* MIDICommands[])
  _presetName = presetName;
  _MIDICommands = MIDICommands;
}

_MIDICommnads is declared like this: MIDICommand* _MIDICommands[];.

When run, this throws the error below on the second line in the constructor:

incompatible types in assignment of 'MIDICommand**' to 'MIDICommand* [0]'

Why do I need to declare _MIDICommands as MIDICommands**, and not MIDICommand* _MIDICommands[]?

The array I pass is declared like this:

MIDICommand* one =  new MIDICommand(1, 2);
MIDICommand* two =  new MIDICommand(2, 2);

MIDICommand* MIDICommandsA[] = { one, two };

EDIT: When I pass in an array, should I just use MIDICommand* MIDICommands, or do I need the [] at the end? Now I've taken them off, but then I get trouble with passing the array.

It doesn't find a matching function to call. Basically, this MIDICommand* MIDICommandsA[] = { one, two }; doesn't match this MIDICommand* MIDICommands parameter.

EDIT 2: So, I've solved the problem of passing the array. I changed the parameter in the constructor to MIDICommand** MIDICommands, and I just pass a pointer to the array.

Now the constructor looks like this:

Preset::Preset(char* presetName, MIDICommand** MIDICommands)
{
  Serial.println(MIDICommands[0]->getProgramChange());

  _presetName = presetName;
  _MIDICommands = *MIDICommands;

  Serial.println(_MIDICommands[0]->getProgramChange());
}

I'm getting an error on the second Serial.println, telling me it is a non-pointer type. I'd like it to be a pointer, so that's what I am looking at now.

_MIDICommands is now declared like this: MIDICommand* _MIDICommands.

EDIT 3: When I try to loop through the array now, it seems I have lost the second element, and have only the first.

  • 1
    Guessing, try leaving off the square brackets in the list of constructor parameters. If no one answer w/the correct solution, I'll try to dig into this more later. – st2000 May 1 '17 at 15:01
  • One is a zero-sized array of pointers, the other is a pointer to an array of pointers. Subtle difference, but important to the compiler. – Majenko May 1 '17 at 16:04
  • @st2000 I tried leaving of the square brackets, but then I have some trouble passing the array. – ptf May 1 '17 at 17:04
  • @Majenko Thanks, but I still haven't figured it out. – ptf May 1 '17 at 17:04
  • Using new on a 2KB RAM system is a sure way to avoid the millis rollover problem. – user31481 Oct 30 '17 at 22:35
1

_MIDICommnads is declared like this: MIDICommand* _MIDICommands[];.

This is the major error. That is an incomplete array type. The number of elements (and storage) is unknown.

To solve this you have decide on how the elements should be stored. Should the class allocate storage and copy the values given to the constructor or should a pointer to the vector be used.

One alternative is to use a template class and provide the max element size as a template class parameter.

Cheers!

  • I've changed that to MIDICommand** _MIDICommands now. Is that not viable as well? I'd rather pass around a pointer to the array of pointers, and it seemed to work, except I can't access my values, so I guess it isn't working in the end. – ptf May 1 '17 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.