static struct tm in a class

Hi all,

I would like to use the tm struct as a static variable.

The date to store would not be the current system time but one manually entered during runtime.

In my class, under Public, i have declared it as:

static struct tm *dataTime;

In the main.cpp, I have tried to define and initialize it with system time temporarily to test out

time_t rawTime;
time ( &rawTime );
tm Indice::dataTime = localtime(&rawTime);

but seems like i can't use time() outside functions.
main.cpp:28: error: expected constructor, destructor, or type conversion before ‘(’ token

How do I initialize values in a static tm of a class?
Last edited on
Create a function that returns a struct tm;

1
2
3
4
5
6
7
tm make_time()
{
    const time_t t = time(0);
    return *localtime(&t);
}

tm Indice::dataTime = make_time();
It works now! Thanks

But I ran into another problem which is to write values into the tm struct

I thought accessing via

Indice::dataTime->tm_year

should be right but got the error

parser.h:121: error: base operand of ‘->’ has non-pointer type ‘tm’

>.< Is the problem lying somewhere else or that using -> to access structs is wrong
tm is not a pointer. It is a struct. Try Indice::dateTime.tm_year.
Topic archived. No new replies allowed.