Multiple instances of a class: No "new" keyword?

Hello!
A bit of history for you to help understand my part of the issue here:

I had created a Sudoku Grid Application with Bloodshed Software’s “Dev-Cpp” many years ago for Windows XP, i believe. I was using and extending the classic built-in “Windows GUI example project” and doing some Mouse/Keyboard event handling by GetMessage() and other Windows API stuff.
But that is about all i have in terms of C/C++ experience.
I have no distinction which features are C and which are C++, that’s why i’m throwing them in a bucket together.
Ahead of time, sorry if i’m missing a possibly obvious thing here, but i’m as new to C/C++ as i am to Arduino programming.

Anyways. On to my question:

I have a PCB (from the company that smiles the whole alphabet) with 4 daisy-chained MAX7219 8x8 LED modules
(That is 32x8 LED "pixels). They’re interfaced using a Serial-to-Parallel Shift Register approach.
Wanting to interact with that, i have created a MAX7219 state machine/control class.

I’m using Visual Studio Code by the way, with the PlatformIO IDE installed.

I am trying to create multiple instances of that class (let’s call the class “max7219”).
Its Constructor takes the index (position) of the current 8x8 block inside the daisy-chain, and a parameter that tells the class (and the MAX7219 chip) to operate in dot matrix mode and not use seven-segment digit decoding.
This is part of my max7219.cpp:

#define OP_MODE_MATRIX 1
#define OP_MODE_SEVENS 2
[...]
max7219::max7219(unsigned byte nIndex, byte nOpMode) { 
  [...]
}

What i would have done in C(++) is:
main.cpp:

[...]
#define MAX_MODULES 4

max7219 * matrix[MAX_MODULES];
void setup() {
 for(unsigned byte nModule=0;nModule<=MAX_MODULES;nModule++) {
   matrix[nModule]=new max7219(nModule, OP_MODE_MATRIX)
 }

Okay, that didn’t work. PlatformIO mocks the line
matrix[nModule]new max7219(nModule, OP_MODE_MATRIX)
with

"Can not access ""max7219::max7219(nModule, OP_MODE_MATRIX)", declared in line 56 of "(path here)"". [b]C/C++(330)[/b]

Does “new” not exist in the Arduino universe, or does it work differently?

If so, how would you tackle multi-instantiation of a class and storing the object references in an array?
I know with the classes for e.g. a 2-line, 20-character LCD screen, i would use:

LiquidCrystal_I2C lcd(0x27,20,4);

(from the LiquidCrystal_I2C example Sketch “HelloWorld”)

But that creates just one instance…
What if i want to use multiple instances? Do i really have to do the

max7219 Module0(0,OPMODE_MATRIX);
max7219 Module1(1,OPMODE_MATRIX);
max7219 Module2(2,OPMODE_MATRIX);
max7219 Module3(3,OPMODE_MATRIX);
max7219 Module4(4,OPMODE_MATRIX);
max7219 Module5(5,OPMODE_MATRIX);

if i want 6 instances?

What about

for(unsigned byte nModule=0;nModule<=3;nModule++) {
  matrix[nModule]=malloc(sizeof(max7219));
}

?
But i can’t call the max7219::max7219() constructor on these memory areas, can I?
Because these are just empty memory, no class or instance info yet in them, right?
So, how would i create an arbitrary count of new instances and make the runtime store them to the malloc’ed areas?
Would I have to do that before setup(), like with the LiquidCrystal_I2C example)?
Please help me understand how classes and instances work on Arduino C/C++.

I’m lost.

Don't post snippets (Snippets R Us!)

what's the exact compiler error you get?

I wasn't finished writing the whole thing yet. I hit Ctrl+LeftArrow to put my cursor a word to the left, and suddenly was facing the text as a published posting. Please have a look at my post again, i have finished and posted it meanwhile.

dermarkus1982:
Does "new" not exist in the Arduino universe, or does it work differently?

Arduino is programmed in C++ and the 'new' operator does indeed exist in that language. So, you're obviously using it incorrectly. But, since you posted incomplete code, telling you how would only be speculation.

**Forget it. **

I myself must have forgotten something:
to save max7219.h to disk after inserting a “public:” before the line that declares the constructor.
My computer had crashed a while after that edit, so the change apparently didn’t last.
Now it works, at least the IDE doesn’t throw any wiggly lines at me.
I didn’t build yet, as there was nothing to build; just a skeleton.

Why didn’t PlatformIO tell me thet the default constructor isn’t “public”? ← Just a rhetoric question;
ignore that please.

Thanks for your time anyway!

How about:

max7219 Modules[6] = 
{
  max7219(0,OPMODE_MATRIX),
  max7219(1,OPMODE_MATRIX),
  max7219(2,OPMODE_MATRIX),
  max7219(3,OPMODE_MATRIX),
  max7219(4,OPMODE_MATRIX),
  max7219(5,OPMODE_MATRIX)
};

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.