Using objects in struct

Jul 3, 2014 at 9:27am
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 Jul 3, 2014 at 9:30am
Jul 3, 2014 at 9:44am
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);
 };
Jul 3, 2014 at 9:53am
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
Jul 3, 2014 at 11:01am
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.
Jul 3, 2014 at 8:50pm
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)
{;}
Jul 3, 2014 at 11:45pm
Thanks. I am oretty new to struct. Could you show mw how to put that into my code?
Jul 5, 2014 at 3:01pm
Just cut and paste the two pieces of code in my post.
Topic archived. No new replies allowed.