Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize a static struct in c++?

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++

like image 868
filippos Avatar asked Apr 18 '11 19:04

filippos


3 Answers

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

This should be enough:

AndroidTimerModel::Resources AndroidTimerModel::ResData;
like image 150
Ben Voigt Avatar answered Oct 19 '22 20:10

Ben Voigt


You can use a struct initializer in C++, but only in the pre-C99 style (i.e, you cannot use designated initializers). Designated intializers, which allow you to specify the members to be initialized by name, rather than relying on declaration order, were introduced in C99, but aren't part of any C++ standard at the moment (belying the common assumption that C++ is a superset of C).

If you are willing to write non-portable C++ code that specifically targets g++, you can always use the GCC-specific extension which has the same functionality as designated constructors. The syntax is like this:

struct value_t my_val = { member_a: 1, member_b: 1.2f };

This reference provides a pretty good overview of both types of initialization in the C context.

Here's an excerpt that shows both the earlier (without designators) and C99 styles:

When initializing a struct, the first initializer in the list initializes the first declared member (unless a designator is specified) (since C99), and all subsequent initializers without designators (since C99) initialize the struct members declared after the one initialized by the previous expression.

struct point {double x,y,z;} p = {1.2, 1.3}; // p.x=1.2, p.y=1.3, p.z=0.0
div_t answer = {.quot = 2, .rem = -1 };      // order of elements in div_t may vary

In some cases you may need to write some code to initialize a structure, and in this case you can use the result of a function, like:

struct Resources AndroidTimerModel::ResData = function_that_acts_like_a_constructor();
like image 9
BeeOnRope Avatar answered Oct 19 '22 21:10

BeeOnRope


You need to declare and define a constructor for struct Resources. eg

struct Resources{
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
    Resources():membFunct(0), data(0), size(0), time(0) {}
    ....
};
like image 1
quamrana Avatar answered Oct 19 '22 19:10

quamrana