How to Define Arrays of Classes

Attempting to define and array ti, of three TicSerial objects. This will not compile. I believe I have done it with other compilers/processors.
The two commented-out lines compile just fine.

Any help appreciated
Thanks-fritz

#include <Tic.h>
#include <SoftwareSerial.h>
SoftwareSerial ticSerial(10, 11);
// TicSerial tic14(ticSerial,14); //X motor
// TicSerial tic15(ticSerial,15); //Y motor
TicSerial ti[3];
void setup(){}
void loop(){}

Tic.h (41.7 KB)

Pelase edit your post and enclose your code inside "code" tags, or your sketch will be damaged by the forum trying to "interpret" special characters.
Thanks.

1 Like

The two lines that compile "just fine" supply arguments to the class's constructor, your attempt at defining an array of the class doesn't. Perhaps the class doesn't have a default constructor?

For further help, supply complete code, inline using code tags. Don't attach files, put the code inline.

You could do

SoftwareSerial ticSerial(10, 11);

TicSerial tic[] = {
  TicSerial(ticSerial, 14),
  TicSerial(ticSerial, 15),
  TicSerial(ticSerial, 16)
};

Or less verbose

SoftwareSerial ticSerial(10, 11);

TicSerial tic[] = {
  { ticSerial, 14 },
  { ticSerial, 15 },
  { ticSerial, 16 }
};

Or if you want the individual instances and an array of pointers

SoftwareSerial ticSerial(10, 11);

TicSerial ticX(ticSerial, 14);
TicSerial ticY(ticSerial, 15);
TicSerial ticZ(ticSerial, 16);

TicSerial* tics[] = { &ticX, &ticY, &ticZ };

1 Like

Thanks all. First of all apologies for omitting to use the /CODE delimiters. I will try to remember (i don't get here often!)
J-M-L gave several versions that work. As gfvalvo mentioned I guess my class was not initializing. I am set up now with this using the first which is intuitively comfortable. I only come out of hibernation every few years to write C++ code and always forget a lot. Rifling thru my old yellowed books (i still use those!) , I surprisingly did not find this issue.
Thanks again
Fritz

Your class’s constructor seems to expect two parameters so you do need to pass them upon instantiation indeed.