Using objects in struct

I am using a library called RunningAverage. To create a instance of RunningAverage you use

RunningAverage myAvg(n)

where n is the number of elements.

I would like to create a struct using RunningAverage like

struct averages {
RunningAverage seconds(60);
RunningAverage minutes(60);
RunningAverage hours.(24);
}

This wont compile for me


Some guidance would be appreciated.


Last edited on
What error do you get when you attempt to compile?

Is it as simple as needing to put a semi-colon after the struct declaration:

1
2
3
4
5
struct averages {
 RunningAverage seconds(60);
 RunningAverage minutes(60);
 RunningAverage hours.(24);
 };
here is the code

#include <RunningAverage.h>

struct Averages
{
RunningAverage seconds(60);
RunningAverage minutes(60);
RunningAverage hours(24);
} Sensor1, Sensor2;


void setup() {
Sensor1.seconds.clear();
}

void loop() {
// put your main code here, to run repeatedly:

}

compiler error message


BareMinimum:5: error: expected identifier before numeric constant
BareMinimum:5: error: expected ',' or '...' before numeric constant
BareMinimum:6: error: expected identifier before numeric constant
BareMinimum:6: error: expected ',' or '...' before numeric constant
BareMinimum:7: error: expected identifier before numeric constant
BareMinimum:7: error: expected ',' or '...' before numeric constant
BareMinimum.ino: In function 'void setup()':
BareMinimum:12: error: 'Sensor1.Averages::seconds' does not have class type
you cannot write it like so. RunningAverage seconds(60); is interpreted as a function prototype. You need a constructor in order to initialize the variables.
You need a constructor for Averages.
1
2
3
4
struct Averages {
    Averages(int s, int m, int h);
    RunningAverage seconds, minutes, hours;
};


In it, you need to explicitly tell the compiler which constructors to use when constructing seconds, minutes, hours. The syntax is a little obscure:
1
2
3
Averages::Averages(int s, int m, int h) :
    seconds(s), minutes(m), hours(h)
{;}
Thanks. I am oretty new to struct. Could you show mw how to put that into my code?
Just cut and paste the two pieces of code in my post.
Topic archived. No new replies allowed.