I would like to create an array of a generic type so I can use multiple sensors and make my code as extensible as possible.
/*
Planning for good design. Agent library
should be able to use more than one sensor.
The agent's constructor will accept a vector
of sensors.
*/
class Sensor
{
public:
Sensor(){};
};
My implementation class
#include "Arduino.h"
class UltraSonicSensor: public Sensor {
public:
/*
Constructors
*/
UltraSonicSensor(){};
UltraSonicSensor(int pin){
pin_ = pin;
};
int Sense(){ return 20;};
/*
Measure Distance
*/
float DistanceMeasure(void);
private:
/*
Class only suppports Metric
*/
float microseconds_to_centimeters(void);
int pin_;
long duration_;
};
#endif // SROBOT_SENSORS_ULTRASONIC_H_
I have tried many, many variations of this code but always the same error
Sensor *sensors[2];
UltraSonicSensor uSensor;
sensors[0] = &uSensor;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println("starting...");
delay(1000);
Serial.println("ok");
// delay(2000);
// for (int i = 0; i < sensors.size(); i++) {
//// sensors[i].Sense();
// }
}
The error is always a variation of
main:10: error: 'sensors' does not name a type
sensors[0] = &uSensor;
^
exit status 1
'sensors' does not name a type
I also used the code for a small replacement vector
class but also the same error