Putting an array of typedef in a class

I have a type I want to use:

typedef struct timeAndLux_ {
    long luxValue;
    unsigned long timeStamp;
}timeAndLux_t;

and I want to make a class that uses an array of 5 of these

how do I declare it?

header file:

#ifndef _LightSensor_h
#define _LightSensor_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "arduino.h"

#endif
class LightSensor {
public:
    LightSensor(int sensorNumber, int minLevel, double calibrationValue, double detectionVariance);
    int SensorNumber;
    int MinLevel;
    long GetReading();
    long GetBaseline();
    bool GetLowWatch();
    bool CheckWarning();
    bool EnterNewReadingValue(long reading, unsigned long timeStamp);

private:
    double _Calibration;
    long _Baseline;
    bool _AlarmState;
    bool _ObjectDetected;
    timeAndLux_t _EmptyLuxReadings[5];
    //timeAndLux_t* _EmptyLuxReadings = (timeAndLux_t*)malloc(sizeof(timeAndLux_t) * 5);
    unsigned long _LowWatchTimeStamp;
    bool _LowWatch;
    bool _LowWarning;
    long _Reading;
    bool _LowLightAlarm;
    double _DetectionVariance;
    long GetAveFromArray(long* arrayToMean, int len);
};

#endif

and the start of my CPP is:

#include "LightSensor.h"
//#include <Vector-1.2.1>

typedef struct timeAndLux_ {
    long luxValue;
    unsigned long timeStamp;
}timeAndLux_t;

LightSensor::LightSensor(int sensorNumber, int minLevel, double calibrationValue, double detectionVariance) {

    timeAndLux_t _EmptyReadings[5] = {
       {0, 0},
       {0, 0},
       {0, 0},
       {0, 0},
       {0, 0}
    };
    SensorNumber = sensorNumber;
    MinLevel = minLevel;
    _Calibration = calibrationValue;
    _DetectionVariance = detectionVariance;
}

but I always get an error 'timeAndLux_t' does not name a type

Why is the typedef not in the *.h file ?
The "typedef" is not required. In C++, you may omit the word "typedef" and it will be automatically a typedef.
You can also put the struct definition inside the class.

Since you are using 'timeAndLux_t' in class declaration in the .h file, you have to define ' timeAndLux_t' before the class declaration. The easiest way is to put it above "class LightSensor" in the .h file.

I agree with all of the above:

  • Dump the 'typedef'
  • Put the struct declaration inside the .h file.

In fact, put it inside the class declaration. And, if the timeAndLux_t struct type is only referenced inside the class, make it's declaration private. If it can be used by "user code", make it public and then refer to it (when outside the class) using the Scope Resolution operator (::)

1 Like

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