Array of classes

Is this possible with Arduino 1.0?

This is what I did:

Library Creation:
I created a folder RobotArm in my library directory and created a new .h file in it (RobotArm.h).

In RobotArm.h I tried to create an array of classes with the code:

AF_DCMOTOR motor[NUM_MOTORS];

Library Modification:
In the motor shield's library AFMotor.cpp, I added a default constructor, and an initialization constructor. (The library only had a constructor with 2 parameters passed to it).

AF_DCMotor::AF_DCMotor()
{
}

For AF_DCMOTOR::init(...) I copied and pasted the parameters and code of AF_DCMOTOR::AF_DCMOTOR(...) (the constructor with 2 parameters passed to it).

Array Initialization:
In my sketch file, I added:

for(int i_motors = 0; i_motors < NUM_MOTORS; i_motors++)
motor[i_motors].init(i_motors+1,1,MOTOR12_64KHZ);

inside of void setup().

When I try to verify my sketch I get the errors:

H:\Arduino\arduino-1.0\libraries\RobotArm/RobotArm.h:58: error: 'AF_DCMOTOR' does not name a type
....
sketch_mar14a.cpp: In function 'void setup()':
sketch_mar14a:6: error: 'motor' was not declared in this scope

The line numbers are the lines of the code I've copied and pasted into my post.

Can you post your code? These snippets make it hard to see the real problem. For example, did you include RobotArm.h?

When posting, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Normally I wouldn't instantiate objects inside a .h file, that belongs in a .cpp file really. In other words, don't put the array in the .h file.

I moved all variable initializations to a .cpp file in my library. This is what I have now:

my sketch:

#include <AFMotor.h>
#include <RobotArm.h>

void setup() {
  
for(int i_motors = 0; i_motors < NUM_MOTORS; i_motors++)
  motor[i_motors].init(i_motors+1,1,MOTOR12_64KHZ);  
  
  for(int i_motor = 0; i_motor < NUM_MOTORS; i_motor++)      
    motor[i_motor].setSpeed(0);
}

void loop() {
  
  motor[0].run(FORWARD);      // turn it on going forward
  delay(10);
  motor[0].run(BACKWARD);     // the other way
  delay(10);
  
}

RobotArm.h:

#include <Arduino.h>

#ifndef RobotArm
#define RobotArm

///////////////////////////  MACROS ///////////////////////////////////////////////

#define MAX_SPEED 1
#define MIN_DV_M1 6.0
#define MIN_DV_M2 6.0
#define MIN_DV_M3 6.0
#define MIN_DV_M4 6.0
#define MOTOR_SUPPLY_VOLTAGE 9.0
#define NUM_MOTORS 4

//////////////////////////////////////////////////////////////////////////////////

///////////////////////////  Struct Types ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

struct position_s
{
  int max[NUM_MOTORS];
  int min[NUM_MOTORS];
  int center[NUM_MOTORS];
};

struct speed_s
{
  int max[NUM_MOTORS];
  int min[NUM_MOTORS];
}; 

struct motors_s 
{
  int fb[NUM_MOTORS];
  int fb_pins[NUM_MOTORS];
}; 

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////


#endif

RobotArm.cpp

#include <RobotArm.h>

/////////////////////////////// SYSTEM STRUCTS ////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

 position_s position = 
{
  {245,245,245,245},
  {10,10,10,10},
  {255/2,255/2,255/2,255/2}
};

speed_s speed = 
{
  {1},
  {(MIN_DV_M1/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M2/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M3/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M4/MOTOR_SUPPLY_VOLTAGE*255)},
};

motors_s motors =
{
  {0},{A0,A1,A2,A3}
};

//////////////////////////////////////////////////////////////////////////////////

////////////////////////////// GLOBAL VARIABLES //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

AF_DCMOTOR motor[NUM_MOTORS];


//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

the relevant part of the modified library AFMotor.cpp (no compilation errors)

AF_DCMotor::AF_DCMotor()
{
}

AF_DCMotor::AF_DCMotor(uint8_t num, uint8_t freq) {
  motornum = num;
  pwmfreq = freq;

  MC.enable();

  switch (num) {
  case 1:
    latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM1(freq);
    break;
  case 2:
    latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM2(freq);
    break;
  case 3:
    latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM3(freq);
    break;
  case 4:
    latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM4(freq);
    break;
  }
}

void AF_DCMotor::init(uint8_t num, uint8_t freq) {
  motornum = num;
  pwmfreq = freq;

  MC.enable();

  switch (num) {
  case 1:
    latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM1(freq);
    break;
  case 2:
    latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM2(freq);
    break;
  case 3:
    latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM3(freq);
    break;
  case 4:
    latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // set both motor pins to 0
    MC.latch_tx();
    initPWM4(freq);
    break;
  }
}

void AF_DCMotor::run(uint8_t cmd) {
  uint8_t a, b;
  switch (motornum) {
  case 1:
    a = MOTOR1_A; b = MOTOR1_B; break;
  case 2:
    a = MOTOR2_A; b = MOTOR2_B; break;
  case 3:
    a = MOTOR3_A; b = MOTOR3_B; break;
  case 4:
    a = MOTOR4_A; b = MOTOR4_B; break;
  default:
    return;
  }

I'm now getting errors:

sketch_mar14a.cpp: In function 'void setup()':
sketch_mar14a:6: error: 'motor' was not declared in this scope
sketch_mar14a:9: error: 'motor' was not declared in this scope
sketch_mar14a.cpp: In function 'void loop()':
sketch_mar14a:14: error: 'motor' was not declared in this scope

I tried moving the array of classes declaration to my sketch instead of the .cpp file and I got the additional error:

sketch_mar14a:3: error: 'AF_DCMOTOR' does not name a type

To answer my own questions:

yes it is possible, and

"does not name a type" and "was not declared in the scope" errors refer to the compiler not being able to find something's definition - http://cplusplus.syntaxerrors.info/index.php?title=‘Foo’_does_not_name_a_type

AF_DCMOTOR should have been AF_DCMotor