Go Down

Topic: library, struct, typdef and initialization (Read 1 time) previous topic - next topic

s-light

Hi Forum :-)

i am writing a library ( the idea was to extract some code i will use in the next and some other projects too...)
the functionality is finished in the current project - so it is just the task to extract it into a external library.

I have read the LibraryTutorial as a starting point. (Its really cool! easy understanding!)

So in the original project the function accessed lots of global variables. So my Idea was to pack them in a struct.
But hm - not so easy^^
i have tried with something like:
this definition is written in the header file of the lib.

Code: [Select]
struct someValues
{
word wLevel_Min = 0;
word wLevel_Max = 65535;
word wStep_Up = 1;
word wStep_Down = 1;
word wDelay_Up = 1;
word wDelay_Down = 1;
};

typedef struct someValues svXXX;


if i try to compile my test / example sketch he throws some errors:
Quote

test.h:38: error: ISO C++ forbids initialization of member 'wLevel_Min'
test.h:38: error: making 'wLevel_Min' static
test.h:38: error: ISO C++ forbids in-class initialization of non-const static member 'wLevel_Min'


if i am interpreting this right - i am not able to initialize my struct to standard values?

in the example sketch i have something like:
Code: [Select]
/**
  * Values for a Testing Thing
  **/
const svXXX someNiceNameing;
someNiceNameing.wLevel_Min =     0;
someNiceNameing.wLevel_Max = 65535;
someNiceNameing.wStep_Up =  1;
someNiceNameing.wStep_Down =  1;
someNiceNameing.wDelay_Up =  5;
someNiceNameing.wDelay_Down = 15;


is this the right idea or iam on the absolut wrong way with the hole struct? :~

sunny greatings
stefan

s-light

#1
Feb 23, 2012, 07:58 pm Last Edit: Feb 23, 2012, 08:59 pm by s-light Reason: 1
Ok found out some things:

initialization is not allowed.

initialization in test sketch looks now this way:
Code: [Select]

/**
 * Values for a Testing Thing
 **/
const svXXX someNiceNameing;

void setup()
{
//other stuff...

someNiceNameing.wLevel_Min =     0;
someNiceNameing.wLevel_Max = 65535;
someNiceNameing.wStep_Up =  1;
someNiceNameing.wStep_Down =  1;
someNiceNameing.wDelay_Up =  5;
someNiceNameing.wDelay_Down = 15;

//other stuff...
}


this looks all good
:)
sunny greetings
stefan

EDIT:
OK - I have just figured out a Problem:
with the way I do the Initialization of 'someNiceNameing' it is to late :-)
just after declared the variable i 'create' something like:
Code: [Select]

/**
  * Values for a Testing Thing
  **/
svXXX someNiceNameing;

ALib myalib(someNiceNameing);
//...

so the things i initialize the someNiceNameing with dont gets into my ALib because it just gets the original state after svXXX in Empty (undefined) state.

so how can i make this the way i want? with pointers? or is there a other possibility to get it working?
i just need the someNiceNameing once to give the values to myalib.... (as const)

Go Up