Structure raises linker exception multiple definition of
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Structure raises linker exception multiple definition of

Threaded View

  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 02:27 PM. Reason: added code tags

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center