declaring an array in a structure with a #define length

Discussion in 'C++' started by athanasios.silis@gmail.com, Jul 28, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Tim Love Guest

    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.
     
    Tim Love, Jul 28, 2006
    #2
    1. Advertising

  3. gangs Guest

    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.
     
    gangs, Jul 28, 2006
    #3
  4. Guest

    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:
    > 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, 2006
    #4
  5. Howard Guest

    <> wrote in message
    news:...
    > 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
     
    Howard, Jul 28, 2006
    #5
  6. Howard Guest

    <> wrote in message
    news:...
    > 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
     
    Howard, Jul 28, 2006
    #6
  7. Howard Guest

    <> wrote in message
    news:...

    >
    > /*------------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
     
    Howard, Jul 28, 2006
    #7
  8. Ian Collins Guest

    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.
     
    Ian Collins, Jul 28, 2006
    #8
  9. nass Guest

    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:
    > 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.
     
    nass, Jul 29, 2006
    #9
  10. Old Wolf Guest

    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 .
     
    Old Wolf, Jul 29, 2006
    #10
  11. Ian Collins Guest

    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.
     
    Ian Collins, Jul 29, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    23
    Views:
    848
    Chris Thomasson
    Aug 29, 2007
  2. Tricky

    Declaring array of length 1

    Tricky, Sep 19, 2008, in forum: VHDL
    Replies:
    4
    Views:
    813
  3. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  4. Tuan  Bui
    Replies:
    14
    Views:
    478
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. sonic
    Replies:
    5
    Views:
    94
Loading...

Share This Page