Pages: [1]   Go Down
Author Topic: library, struct, typdef and initialization  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
/**
  * 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? smiley-confuse

sunny greatings
stefan
Logged

Germany
Offline Offline
Jr. Member
**
Karma: 0
Posts: 73
just sun
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok found out some things:

initialization is not allowed.

initialization in test sketch looks now this way:
Code:
/**
  * 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
 smiley
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:
/**
  * 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)
« Last Edit: February 23, 2012, 02:59:28 pm by s-light » Logged

Pages: [1]   Go Up
Jump to: