By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

declaring an array in a structure with a #define length

P: n/a
Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

Jul 28 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
at**************@gmail.com writes:
>Hello everyone,
i am attempting to make a structure
>#include "globalVars.h"
>struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];
>} saveVars, getVars;
>and i have #define numOfGlobalVars in globalVars.h... it does not
compile
It works for me. I suggest you post a minimal but complete example.
Jul 28 '06 #2

P: n/a

at**************@gmail.com wrote:
Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass
It worked for me. Tell us a little more about the compiler you've used
the version and stuff.

gangs.

Jul 28 '06 #3

P: n/a
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
/*------------fileIO.h----------------------------------------------------------*/

#ifndef FILEIO_H
#define FILEIO_H

void writeToFile(const void *buffer,const int &unitSize,const char
*filename="../globalsFile.txt");

int readFromFile(void *buffer,const long bufSize,const char
*filename="../globalsFile.txt");

#endif //FILEIO_H

/*------------fileIO.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include <stdio.h>

void writeToFile(const void *buffer,const int &unitSize,const char
*filename)
{
FILE * pFile;
pFile = fopen (filename,"wt");
fwrite(buffer,unitSize,1,pFile);
fclose(pFile);

}

int readFromFile(void *buffer,const long bufSize,const char *filename)
{
FILE * pFile;
pFile = fopen (filename,"rt");

if (pFile==NULL) return (0);

fread (buffer,bufSize,1,pFile);

fclose (pFile);

return (1);
}

/*------------main.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include "../globalVars.h"
#include <iostream.h>

struct myStruct{
int offset;
//unsigned char uChars[numOfGlobalVars-1];
unsigned char uChar1;
unsigned char uChar2;
unsigned char uChar3;
unsigned char uChar4;
} saveVars, getVars;

int main()
{
//init the global variables
appDateTimeOffset=99;
operationalState=96;
logSamplingPeriod=101;
logType=75;
changeLog=69;

//fill up the structure with the globalVariables values
saveVars.offset=appDateTimeOffset;
saveVars.uChar1=operationalState;
saveVars.uChar2=logSamplingPeriod;
saveVars.uChar3=logType;
saveVars.uChar4=changeLog;

writeToFile(&saveVars,sizeof(saveVars));

//reinit the globalVars
appDateTimeOffset=0;
operationalState=0;
logSamplingPeriod=0;
logType=0;
changeLog=0;

if (readFromFile(&getVars,sizeof(getVars)))
cout<<"all OK"<<endl;

//refill the globalVars with the readFromFile values
appDateTimeOffset=getVars.offset;
operationalState=getVars.uChar1;
logSamplingPeriod=getVars.uChar2;
logType=getVars.uChar3;
changeLog=getVars.uChar4;

cout<<appDateTimeOffset<<endl;
cout<<operationalState<<endl;
cout<<logSamplingPeriod<<endl;
cout<<logType<<endl;
cout<<changeLog<<endl;

}//main

/*-----------------------------END-------------------------------------------------------------*/

i have switched to 4 independent entries (the uChar# declarations) in
order to make it work... but thats pretty much it.. if you want to
compile it just change the uChar# to uChars[#]..
Thank you for your help.
nass
gangs wrote:
at**************@gmail.com wrote:
Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.

should i not be able to do that?

nass

It worked for me. Tell us a little more about the compiler you've used
the version and stuff.

gangs.
Jul 28 '06 #4

P: n/a

<at**************@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hello everyone,
i am attempting to make a structure

#include "globalVars.h"

struct myStruct{
int offset;
unsigned char uChars[numOfGlobalVars -1];

} saveVars, getVars;
and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying

main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.
Are you using "struct myStruct" as a local variable or parameter type
somewhere? Try just "myStruct" as the type. This is C++, not C. (Right?)

(Also, next time, post the code that's causing the problem. You've left out
main, where the error message says the problem is.)

-Howard

Jul 28 '06 #5

P: n/a

<at**************@gmail.comwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H

<snipped fileIO stuff>
/*------------main.cpp----------------------------------------------------------*/

#include "fileIO.h"
#include "../globalVars.h"
#include <iostream.h>

struct myStruct{
int offset;
//unsigned char uChars[numOfGlobalVars-1];
unsigned char uChar1;
unsigned char uChar2;
unsigned char uChar3;
unsigned char uChar4;
} saveVars, getVars;

int main()
{
//init the global variables
appDateTimeOffset=99;
operationalState=96;
logSamplingPeriod=101;
logType=75;
changeLog=69;

//fill up the structure with the globalVariables values
saveVars.offset=appDateTimeOffset;
saveVars.uChar1=operationalState;
saveVars.uChar2=logSamplingPeriod;
saveVars.uChar3=logType;
saveVars.uChar4=changeLog;

writeToFile(&saveVars,sizeof(saveVars));

//reinit the globalVars
appDateTimeOffset=0;
operationalState=0;
logSamplingPeriod=0;
logType=0;
changeLog=0;

if (readFromFile(&getVars,sizeof(getVars)))
cout<<"all OK"<<endl;

//refill the globalVars with the readFromFile values
appDateTimeOffset=getVars.offset;
operationalState=getVars.uChar1;
logSamplingPeriod=getVars.uChar2;
logType=getVars.uChar3;
changeLog=getVars.uChar4;

cout<<appDateTimeOffset<<endl;
cout<<operationalState<<endl;
cout<<logSamplingPeriod<<endl;
cout<<logType<<endl;
cout<<changeLog<<endl;

}//main

/*-----------------------------END-------------------------------------------------------------*/

i have switched to 4 independent entries (the uChar# declarations) in
order to make it work... but thats pretty much it.. if you want to
compile it just change the uChar# to uChars[#]..
Thank you for your help.
nass

Please don't top-post. Post responses below or interspersed with what
you're quoting. And trim off extra stuff that's not needed, ok? (Thanks.)

Where is numOfGlobalVars defined? I don't see it anywhere here. If you've
left it out, you'll get more than just the one error you asked about.
Perhaps you're also getting an error that numOfGlobalVars is undefined?

One suggestion: don't use globals unless you have to. Why not declare
getVars and saveVars as local variables in main? I see no reason they need
to be globals.

-Howard

Jul 28 '06 #6

P: n/a

<at**************@gmail.comwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...
>
/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H

This probably isn't a good idea. For one thing, it's best to avoid global
variables whenever possible. These could all have gone inside your main()
function. There's no need to make them globals.

Second, if you ever include this header file from another unit, you'll get
an "undefined symbol" error, because of the include guards. And removing
the include guards would result in "muliply defined" errors.

If you really need to use global variables, and if you need a header file
you can include in multiple units to see those variables, then here's what
you do: Declare the global variables in an implementation (.cpp) file.
Then, declare them as "extern" in the header file. This lets everyone who
includes the header file see those symbols, without getting multiple
definition errors.

But as I said, try to avoid using globals in the first place, and life
(well, at least your programming life) will be much easier.

-Howard
Jul 28 '06 #7

P: n/a
at**************@gmail.com wrote:
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
As others have said, putting the definitions of globals in a header is a
bad idea.

One more thing, if you variables have a fixed set of valid values, use
enums.

--
Ian Collins.
Jul 28 '06 #8

P: n/a
People,
i do appreciate all your efforts to pull me away from using global
variables and i can assure you i don't often.. the code i posted here
is trivial however, just a test main.cpp to make sure that fileIO.h,
fileIO.cpp work correctly... so yes, these 2 structures are not
defined globally there.. they did not need to indeed...
however some other important variables declared in globalVars.h file
are assigned values by forms that are are generated at run time and
also get destroyed then to free up memory.. and it would get messy if i
tried to include every header file containing an extern declaration to
every other simply cause i would have declared a variable in their
corresponding cpp...

anyhow you tell me that i have posted in complete code.. i mean come
on, did you read what i have included? main.h is there, and
globalVars.h is there too along with fileIO.h and fileIO.cpp... in fact
i was thinking i wrote too much code... however since i know that this
main.cpp SHOULD compile i decided to post it all just in case i have
overlooked smth else that causes the error in compilation.

once again i thank you for your advice - my programming experience is
very limited, yet i know to not globalise everything. still lets focus
on the fact that
struct myStruct {unsigned char uChar[numOfGlobalVars-1];} does not
compile.
nass

Ian Collins wrote:
at**************@gmail.com wrote:
hello again
g++ 3.3.6 compiler on slackware linux but
i am using the qmake utility of QtDesigner to generate the makefile,
then i just type make...
still no qlibraries are used in this part of the program...
here im sending u the fileIO.h and fileIO.cpp as well as a globalVars.h
and a main.cpp that i made to test the fileIO files:

/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
As others have said, putting the definitions of globals in a header is a
bad idea.

One more thing, if you variables have a fixed set of valid values, use
enums.

--
Ian Collins.
Jul 29 '06 #9

P: n/a
at**************@gmail.com wrote:
>and i have #define numOfGlobalVars in globalVars.h... it does not
compile saying
>main.cpp:23: error: 'struct myStruct' has no member named 'uChars',
when i try to access these uChars later on in my code.
/*------------globalVars.h----------------------------------------------------------*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

int appDateTimeOffset;
unsigned char operationalState; //0=auto, 1=manual,
2=scheduled, 3=logging
unsigned char logSamplingPeriod; //0=off, 1=1min, 2=10min,
3=30min, 4=1hour
unsigned char logType; //0=cyclical, 1=linear
unsigned char changeLog; //0=per1min, 1=per1hour,
2=per1day

#endif //GLOBALVARS_H
There is no define numOfGlobalVars in globalvars.h .

Jul 29 '06 #10

P: n/a
nass wrote:

Please don't top post, it's considered rude on Usenet.
People,
i do appreciate all your efforts to pull me away from using global
variables and i can assure you i don't often.. the code i posted here
is trivial however, just a test main.cpp to make sure that fileIO.h,
fileIO.cpp work correctly... so yes, these 2 structures are not
defined globally there.. they did not need to indeed...
however some other important variables declared in globalVars.h file
are assigned values by forms that are are generated at run time and
also get destroyed then to free up memory.. and it would get messy if i
tried to include every header file containing an extern declaration to
every other simply cause i would have declared a variable in their
corresponding cpp...

anyhow you tell me that i have posted in complete code.. i mean come
on, did you read what i have included? main.h is there, and
globalVars.h is there too along with fileIO.h and fileIO.cpp... in fact
i was thinking i wrote too much code... however since i know that this
main.cpp SHOULD compile i decided to post it all just in case i have
overlooked smth else that causes the error in compilation.
By incomplete, people mean that what you have posted ether won't
compile, or compiles, but doesn't exibit your problem. Your code (when
combined in one file) as posted is an instance of the latter.
once again i thank you for your advice - my programming experience is
very limited, yet i know to not globalise everything. still lets focus
on the fact that
struct myStruct {unsigned char uChar[numOfGlobalVars-1];} does not
compile.
You might have missed one important bit of advice - don't put
non-constant variable definitions in a header.

No one can answer your question without knowing what numOfGlobalVars is.

--
Ian Collins.
Jul 29 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.