CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    20

    Structure raises linker exception multiple definition of

    As I read from some online examples structures should work like this:
    Code:
    struct nameOfStruct
    {
    int myMember1;
    short myMember2;
    };
    
    int main()
    {
    nameOfStruct myInstance;
    myInstance.myMember = 444;
    return 1;
    }
    This code doesn't link good:
    Undefined reference to nameOfStruct::myMember1
    First question, why?

    So i found some example and "repaired" my code:
    Code:
    struct nameOfStruct
    {
    int myMember1;
    short myMember2;
    };
    
    int nameOfStruct::myMember1;
    
    int main()
    {
    nameOfStruct myInstance;
    myInstance.myMember = 444;
    return 1;
    }
    Now code compiles well.
    As my application developed, I came to the point when I must make this type (structure) visible to multiple units. So i tried to do this:
    Code:
    //Structures.h
    struct nameOfStruct
    {
    int myMember1;
    short myMember2;
    };
    
    int nameOfStruct::myMember1;
    Code:
    //Functions.h
    void Function1(nameOfStruct myStructArgument);
    Code:
    //Functions.cpp
    #include "Structures.h"
    
    void Function1(nameOfStruct myStructArgument)
    {
    //code
    }
    Code:
    //Main.cpp
    #include "Structures.h"
    #include "Functions.h"
    
    int main()
    {
    nameOfStruct myInstance;
    Functions1(myInstance);
    myInstance.myMember = 444;
    return 1;
    }
    And so i get linker error: Multiple definition of nameOfStruct::myMember.
    Second question, why?

    Thanks for help
    Last edited by Vila; April 16th, 2010 at 01:27 PM. Reason: added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Structure raises linker exception multiple definition of

    You should post the actual code that you tried. In your first example, you should be getting a compile error, not a linker error, because the struct has no member named myMember.

    Compile and link this program:
    Code:
    struct nameOfStruct
    {
        int myMember1;
        short myMember2;
    };
    
    int main()
    {
        nameOfStruct myInstance;
        myInstance.myMember1 = 444;
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2007
    Posts
    20

    Re: Structure raises linker exception multiple definition of

    That's a typo i made while typing code to this site. Code is flawless in my actual program and members don't have generic names like myMember1. Still i have:

    Undefined reference to nameOfStruct::myMember1

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Structure raises linker exception multiple definition of

    Unfortunately, that does not make sense. It might make sense if member functions are involved, but the code is flawless, right?

    So, post your actual code.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2007
    Posts
    20

    Re: Structure raises linker exception multiple definition of

    Yeah it is to my eye, but not to my linker xD
    Only for you i made this code 2 seconds ago and tried to compile:

    Code:
    //main.cpp
    #include <iostream>
    
    using namespace std;
    
    //command interpreting
    typedef struct COMMAND
    {
           static bool files;
           static string filename;
           static short aLevel;
           static short wLevel;
           static short aaLevel;
           static short lLevel;
           static bool report;
    };
    
    int main(void)
    {
           struct COMMAND command;
           command.files = true;
           return 1;
    }
    Exact message I get is:
    [Linker error] undefined reference to `COMMAND::files'

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Structure raises linker exception multiple definition of

    Ah, now we're talking

    The big difference between this new code snippet and what you posted earlier is that the member variables are static members. Generally, static member variables are declared in the class definition (in a header) and then defined in a source file. This explains the multiple definition errors: you defined them in a header, and then included the header in more than one source file, thus the same static members were defined in more than one translation unit.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jul 2007
    Posts
    20

    Re: Structure raises linker exception multiple definition of

    Ok I make them non-static (i made them static while attempting to solve my problem xD and obviously buried my self even deeper), and now when i removed that I compiled my code without any errors. Now i am confused what first error was so i putted that members static. Oh s*it i am so confused... My first week in C++, it's nice language just if you don't do something right you get yourself dizzy. Anyway thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured