10

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

filippos
  • 111
  • 1
  • 2
  • 4

6 Answers6

9

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();
Community
  • 1
  • 1
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
9

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

This should be enough:

AndroidTimerModel::Resources AndroidTimerModel::ResData;
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

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) {}
    ....
};
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

You need to initialise the whole struct variable, something like this:

AndroidTimerConcept::Resources AndroidTimerModel::ResData = { NULL, NULL, 0, 0 };
pmdj
  • 22,018
  • 3
  • 52
  • 103
  • That initializer has no effect, all variables with static lifetime are zero-initialized before execution begins. – Ben Voigt Apr 18 '11 at 19:24
  • 1
    True, though if OP wants to initialise any of the fields to a non-zero value, this is the syntax to use. – pmdj Apr 18 '11 at 19:35
0

Is it AndroidTimerModel or AndroidTimerConcept, you can't use different names and expect the compiler to think they're the same thing.

You need to scope the name Resources, it's not in global scope, it's in the scope of the AndroidTimerModel class:

AndroidTimerModel::Resources AndroidTimerModel::ResData;

I suggest you give Resources a constructor:

struct Resources{
    Resources(timer_delegate_t aMembFunct, void* aData, int aSize, millis_t aTime )
      : membFunc(aMembFunct)
      , data(aData)
      , size(aSize)
      , time(aTime)
    {}
    timer_delegate_t membFunct;
    void *data;
    int size;
    millis_t time;
};

And you can then define Res in your .cpp as:

AndroidTimerModel::Resources AndroidTimerModel::ResData(/* params here */);
Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • sorry it is AndroidTimerModel concept appeared because i didn't copy paste the first line of the header when posting the question.. – filippos Apr 18 '11 at 19:41
-4

Why is your struct part of a class? I would make it global outside of the class.

memset(&structname, 0, sizeof(structname)); will initialize your structure to 0.