Thread: extern structure

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    8

    extern structure

    Hello,

    I'm kinda new to C++ and have a lot of trouble with. This is one of those things I can't solve myself...

    I made a structure that I want to use in any cpp file so i don't have to re-declare it everytime i wish to use it. So I tried to make it extern like I did to a few CStrings (Yes I'm working in MFC) which worked perfectly. Here's the code I got atm:

    Code:
    //af editor.h
    
    extern CString currentfile;
    
    struct AFHEADER {
        short m_fighternumber;
    };
    
    extern struct afheader AFHEADER;
    
    //af editor.cpp
    
    CString currentfile;
    struct afheader AFHEADER;
    I made this code after reading a bunch of post and stuff i googled.
    Please help

    ps. Sorry for my english I'm sleeeepy..

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This line:

    extern CString currentfile;

    says to look outside the current block for the definition of currentfile. Since that statement is not inside a block, the search for the defintion of currentfile expands to the other files in your program.

    I made a structure that I want to use in any cpp file so i don't have to re-declare it everytime i wish to use it.
    Then, put your struct in a "header file" and use an include statement to include it in any .cpp file where it is needed:

    #include "myfile.h"

    The include statement will be replaced by myfile.h. That is what the purpose of "header files" is.
    Last edited by 7stud; 06-22-2005 at 03:54 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Here's a more complete code:

    Code:
    //af editor.h
    
    extern CString currentfile;
    
    struct AFHEADER {
        short m_fighternumber;
    };
    
    //af editor.cpp
    include "af editor.h"
    
    CString currentfile;
    extern struct afheader AFHEADER;
    
    //LoadingDlg.cpp
    include "af editor.h"
    
    //blah some code etc..
    
    afheader.m_fighternumber = buffer[0];

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Quote Originally Posted by 7stud
    This line:

    extern CString currentfile;

    says to look for the definition of the variable currentfile outside the file.
    Yes but it works fine

    the structure is the one I'm having problems with

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Yes but it works fine

    the structure is the one I'm having problems with
    1) If extern is an instruction to search outside the current block for the definition of a variable, would it make sense to declare the variable in that block? In other words, if a variable is declared inside a block, why would you give an instruction to go look outside the block for the variable?

    2) Is your struct declared in the same block as the extern statement? If so, why would you need extern?

    3) Is the name you specified for your struct the same name you used to declare a struct?
    Last edited by 7stud; 06-22-2005 at 04:14 PM.

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You are going to have to either redeclare an object of that struct in each file (and include the .h file), or declare an object of that struct in say your main .cpp, then in the other files you want to use it declare it again with an extern in front (which means you are looking for that struct definition from outside the file.)
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by vexon
    Here's a more complete code:

    Code:
    //af editor.h
    
    extern CString currentfile;
    
    struct AFHEADER {
        short m_fighternumber;
    };
    
    //af editor.cpp
    include "af editor.h"
    
    CString currentfile;
    extern struct afheader AFHEADER;
    
    //LoadingDlg.cpp
    include "af editor.h"
    
    //blah some code etc..
    
    afheader.m_fighternumber = buffer[0];
    Code:
    //af editor.h
    
    extern CString currentfile;
    
    struct AFHEADER {
        short m_fighternumber;
    };
    
    //af editor.cpp
    include "af editor.h"
    
    CString currentfile;
    extern struct AFHEADER afheader;
    
    //LoadingDlg.cpp
    include "af editor.h"
    
    //blah some code etc..
    
    afheader.m_fighternumber = buffer[0];
    extern isn't a instruction. It's a keyword that tells the linker that the variable is defined in some other object file, so that the linker will use that variable when being refered.

    you declare a variable in a file
    Code:
    //file1.cpp
    int zzz = 0;
    if you want to use that same variable in another file you declare it as external so the linker will later search for it in another object file.
    Code:
    //file2.cpp
    extern int zzz;
    Last edited by xErath; 06-22-2005 at 05:06 PM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Thanks for all the help but it still doesn't work.
    I'm making this all extern because I want to use it in several cpp's not just one and declaring the struct in a header doesn't seem to work well. I get the following error when I only declare it in a header that's included in all cpp's:

    AF Editor error LNK2005: "struct AFHEADER afheader" (?afheader@@3UAFHEADER@@A) already defined in AF Editor.obj

    I get this one 4 times and another one:

    AF Editor fatal error LNK1169: one or more multiply defined symbols found

    -----

    When I do it like this:

    Code:
    //AF Editor.h
    extern struct AFHEADER {
        short m_fighternumber;
    } afheader;
    
    //AF Editor.cpp
    struct AFHEADER {
        short m_fighternumber;
    } afheader;
    
    //LoadingDlg.cpp
    afheader.m_fighternumber = buffer[0];
    i get this error:

    AF Editor.cpp(35): error C2011: 'AFHEADER' : 'struct' type redefinition

    So I guess the last method is right but I only need to declare the extern structure the way it should be. Only a sample of an extern struct would do it I think just keep my intentions in mind

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You do not need to use extern, and should avoid to do so. You will however have to redeclare the object of the structure you want to use in each .cpp file you wish to use it in, you could use an extern for this part but its not necessary in this example.

    Code:
    //AF Editor.h
    struct AFHEADER {
        short m_fighternumber; // if you want to share this value between the files add 'static' in front
    }; // you cant define the object 'afheader' here, .h is for declarations
    
    //AF Editor.cpp
    #include "Editor.h"
    AFHEADER object1afheader; // an object of AFHEADER now exists in this file
    object1afheader.m_fighternumber = 100; // thats how you use it.
    
    //LoadingDlg.cpp
    #include "Editor.h"
    AFHEADER object2afheader; // an object of AFHEADER now exists in this file
    object2afheader.m_fighternumber = buffer[0]; // thats how you use it.
    I hope you see how you interact between the files now.. and I'd prefer not going into how to use an extern again, its not necessary so I'd just leave it alone for now.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Thanks that helped But i need to share the same values of m_fighternumber between the files. Because if I understand this right i have two objects now with each different values. And that's not what I want.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Nevermind i solved it all now i did use an extern because I believe that's the only way of sharing the same values in multiple cpp's.
    So here's what I did:

    Code:
    //AF Editor.h
    
    struct AFHEADER {
        short m_fighternumber;
    };
    
    extern AFHEADER afheader;
    
    //AF Editor.cpp
    
    AFHEADER afheader;
    And it just works

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm kinda new to C++
    ...then don't use global variables or extern. If you need to call a function that requires a struct, then pass it a struct.

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by vexon
    And it just works
    Thats.. odd, and lucky. It shouldnt work because once you include that .h into multiple .cpp files you will get errors because you're defining something in a .h, and second thats just wierd that it actually worked.

    Also, look in the comments that I put in that code example I gave you, if you need to share the same value of m_fighternumber;, define it like this: static short m_fighternumber; with the code I exampled and it should also work.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Thanks

    btw.. I'm not that new to C++ I'm just still in rookie stage I'm doing C++ for almost one year so heheheh

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'm not that new to C++ I'm just still in rookie stage I'm doing C++ for almost one year so heheheh
    ...then you should know better than to use global variables and extern.

    And it just works
    It doesn't compile for me, and I don't believe it should. In this file:

    Code:
    //AF Editor.cpp
    
    AFHEADER afheader;
    AFHEADER is an unkown entity.
    Last edited by 7stud; 06-23-2005 at 12:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 07:02 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM