Array of objects - conversion to non-scalar type requested

Good evening. What am I doing wrong here. I want to define a list of values to initialise my object with. Is this the wrong approach?

exit status 1
conversion from 'int [5][2]' to non-scalar type 'Servo_class' requested

#define NUMBER_OF_SERVOS  5

int myservos[NUMBER_OF_SERVOS][2] = {
  {3, 4},
  {3, 5},
  {3, 6},
  {3, 7},
  {3, 8}
};

class Servo_class
{
  private: 
    int _servo_cntrlr_num;
    int _servo_pin_num;
   public:
    Servo_class(int servo_cntrlr_num, int servo_pin_num){
      _servo_cntrlr_num = servo_cntrlr_num;
      _servo_pin_num = servo_pin_num;
    }
};
Servo_class servo_array[NUMBER_OF_SERVOS] = myservos; 

void setup() {
}

void loop() {
}

https://www.javatpoint.com/initialise-an-array-of-objects-with-parameterised-constructors-in-cpp

(Compiles, NOT tested!)

#define NUMBER_OF_SERVOS  5
class Servo_class
{
  private: 
    int _servo_cntrlr_num;
    int _servo_pin_num;
   public:
    Servo_class(int servo_cntrlr_num, int servo_pin_num){
      _servo_cntrlr_num = servo_cntrlr_num;
      _servo_pin_num = servo_pin_num;
    }
};

//-------------------------------------------------------------------------------------
Servo_class servo_array[NUMBER_OF_SERVOS] = {
                                              Servo_class(3, 4),
                                              Servo_class(3, 5),
                                              Servo_class(3, 6),
                                              Servo_class(3, 7),
                                              Servo_class(3, 8)
                                            }; 

void setup() {
}

void loop() {
}

hope that helps...

why not simply

#include <Servo.h>

struct MyServo {
    byte    pin;
    int     pos;
    Servo   servo;
};

MyServo myServo [] = {
    { 4,  90 },
    { 5, 180 },
    { 6,   0 },
    { 7, 100 },
    { 8,  45 },
};
const int Nservo = sizeof(myServo)/sizeof(MyServo);


void setup()
{
    for (int n = 0; n < Nservo; n++)  {
        myServo [n].servo.attach (myServo [n].pin);
        myServo [n].servo.write  (myServo [n].pos);
    }
}

void loop()
{
}

Thanks for the prompt replies! Much appreciated.

I was hoping to have the list of initial values at the top of the file and easy to change without having to dig through the code.

Can i do it in some sort of comma delimited list? Something like this....

int myservos[][2] = {{3, 4},{3, 5},{3, 6},{3, 7},{3, 8}};

This is a good idea! Thanks.

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