extern struct? I know its simple but i dont know how.

I just came across a strange gap in my knowledge. I need to declare my struct extern just like the rest of my variables ect.



extern int i;

extern char *s;

//etc...

But How do i do this with a struct?



Thanks.

QueEst


Comments

  • : I just came across a strange gap in my knowledge. I need to declare my struct extern just like the rest of my variables ect.

    :

    : extern int i;

    : extern char *s;

    : //etc...

    : But How do i do this with a struct?



    Did you typedef the structure? I usually do that so I can reuse my structure types.



    Maybe try:



    extern struct structure_type structurevariable;



    -Xotor-


  • : Did you typedef the structure?

    No. I dont need to reuse the structure. Its just defined like this:



    struct

    {

    //somevariables

    } structname;





    : Maybe try:

    : extern struct structure_type structurevariable;

    That didnt work. My struct is called "stat". Its defined in SubFuncs.H which is called by both main,cpp and SubFuncs.Cpp. These are the main errors i get when compiling.



    warning C4091: 'extern ' : ignored on left of 'struct stat' when no variable is declared

    error C2027: use of undefined type 'stat'

    error C2228: left of '.classtype' must have class/struct/union type

    error C2027: use of undefined type 'stat'



    Does anybody know how i should do this? Shouldnt it be simple?


  • Write either



    typedef struct

    {

    //somevariables

    } structname;



    extern structname var;



    or

    struct structname {

    //somevariables

    }



    extern struct structname var;



    that should both be possible.


  • : Write either

    :

    : typedef struct

    : {

    : //somevariables

    : } structname;

    :

    : extern structname var;

    :

    : or

    : struct structname {

    : //somevariables

    : }

    :

    : extern struct structname var;

    :

    : that should both be possible.

    :



    I doesnt work. Maybe it has something to do with the way my files are set up.



    main.cpp -

    #include

    //contains

    struct

    {

    int StructInt;

    char *StructChar;

    } stat;



    SubFuncs.cpp -

    #inclcude



    SubFuncs.H -

    extern ... //AH! Nothing works.



    Does this look right to everyone? Maye i didnt understand your example.

    :extern structname var;

    what is "var"? Is this all the variables included in the struct?



    Sorry to keep bothering everyone.


  • You have to define a structure TYPE before you can extern to it. The other file has no idea what your anonymous structure has inside it. Extern can only be applied to objects, e.g. defined TYPES of data like a function, a structure, a class, a data type etc.



    Therefore you have to do the following:



    //First File:



    typedef struct Structure

    {

    int X;

    int Y;

    float Z;

    } Structure;



    Structure Test;



    //Second file:



    extern struct Structure Test;









    -Xotor-





    : main.cpp -

    : #include

    : //contains

    : struct

    : {

    : int StructInt;

    : char *StructChar;

    : } stat;

    :

    : SubFuncs.cpp -

    : #inclcude

    :

    : SubFuncs.H -

    : extern ... //AH! Nothing works.

    :

    : Does this look right to everyone? Maye i didnt understand your example.

    : :extern structname var;

    : what is "var"? Is this all the variables included in the struct?

    :

    : Sorry to keep bothering everyone.

    :






Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories