How to initialize a static structure in C ++?

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

In the timer.h header file, 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 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 a .cpp file, I get the error message: timer.cpp: 7: error: expected initializer before '. Marker

Any suggestions would be really helpful.

btw i use g ++

+6
source share
6 answers

You do not define individual instance members in a static member.

That should be enough:

 AndroidTimerModel::Resources AndroidTimerModel::ResData; 
+6
source

The structure initializer can be used in C ++, but only in the pre-C99 style (i.e. you cannot use the assigned initializers). Assigned initializers have been introduced in C99 that allow you to specify elements that will be initialized by name, rather than relying on the declaration order, but are not part of any C ++ standard at the moment (adding a general assumption that C ++ is superset of C).

If you want to write non-portable C ++ code specifically designed for g ++, you can always use the GCC extension , which has the same functionality as the designated constructors. The syntax is as follows:

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

This link gives a pretty good overview of both types of initialization in the context of C.

Here is an excerpt that shows both the previous ones (without notation) and the C99 styles:

When initializing the structure, the first initializer in the list initializes the first declared element (if it is not specified (starting from C99), and all subsequent initializers without (starting from C99) initialize the elements of the structure declared after the one that was initialized by the previous expression.

 struct point {double x,y,z;} p = {1.2, 1.3}; // px=1.2, py=1.3, pz=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 code to initialize the structure, in which case you can use the result of a function, for example:

 struct Resources AndroidTimerModel::ResData = function_that_acts_like_a_constructor(); 
+4
source

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) {} .... }; 
+1
source

You need to initialize the entire struct variable, something like this:

 AndroidTimerConcept::Resources AndroidTimerModel::ResData = { NULL, NULL, 0, 0 }; 
0
source

This is AndroidTimerModel or AndroidTimerConcept, you cannot use different names and expect the compiler to think that they are the same.

You need to specify the name "Resources", it does not belong to the global scope, it belongs to the AndroidTimerModel class:

 AndroidTimerModel::Resources AndroidTimerModel::ResData; 

I suggest you provide the constructor with resources:

 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 then you can define Res in your .cpp as:

 AndroidTimerModel::Resources AndroidTimerModel::ResData(/* params here */); 
0
source

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

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

-3
source

Source: https://habr.com/ru/post/886172/


All Articles