how to initialize a static struct in c++?

filippos picture filippos · Apr 18, 2011 · Viewed 35.2k times · Source

I have managed to initialize correct any variable of basic type(i.e. int, char, float etc) but when declaring a little complex variable all i can see is errors.

In the header file timer.h i declare

class AndroidTimerConcept {
...
private:
    //struct that holds the necessary info for every event
    struct Resources{
        timer_delegate_t membFunct;
        void *data;
        int size;
        millis_t time;
    };
    //declaring an array of 10 Resources structs
    static struct Resources ResData;
    static int best;
...
}

inside the timer.cpp file

#include <iostream>
#include "timer.h"
using namespace std;


int AndroidTimerModel::best=1000;
struct Resources AndroidTimerModel::ResData.size; //line 17!!
//constructor that initializes all the necessary variables

AndroidTimerModel::AndroidTimerModel()
{
    signal(SIGALRM,signalHandler);

    for(int i=0; i<MAX_EVENTS; i++)
    {
        //ResData.data=NULL;
        ResData.size=-1;
        //ResData.time=-1;
    }

    best=1000;

}

when compiling the .cpp file i get the error: timer.cpp:7: error: expected initializer before ‘.’ token

Any suggestions would be really helpful.

btw i use g++

Answer

Ben Voigt picture Ben Voigt · Apr 18, 2011

You don't separately define individual instance members within a static member.

This should be enough:

AndroidTimerModel::Resources AndroidTimerModel::ResData;