Instantiate array after declaration


hello all,

i'm new c/c++, might trivial question, haven't been able find proper solution.

how instantiate (and declare length) array after having declared before that? have create global array, gets instantiated in constructor. also, length of array depends on parameter of constructor, don't know length when declaring.

this code:

motor.h
code: [select]
#ifndef motor_h
#define motor_h

#include "wprogram.h"
#include <servo.h>

class motor {

     public:
           motor(int ports[]);
           void setspeed(int speed);
           
           int count;
           servo motor[];

};

#endif


motor.cpp
code: [select]
#include "wprogram.h"
#include "motor.h"

motor::motor(int ports[]) {
     
     // instantiate motors
     this->count = sizeof(ports) / sizeof(ports[0]);
             
             // somehow instantiate array?
       
     for ( int = 0; < this->count; ++i )
           this->motor[i].attach(ports[i]);
     
     // initialize motors
     this->setspeed(179);
     delay(1000);
     this->setspeed(0);
     delay(1000);
}

void motor::setspeed(int speed) {
       for ( int = 0; < this->count; ++i )
             this->motor[i].write(speed);
       
       // give motor time adjust
       delay(15);
}

you mean malloc?


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Instantiate array after declaration


arduino

Comments