CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    8

    About Error LNK2005

    I define some globle variable in my class,and In other class
    I define this class's instance,When linking,
    there show "error LNK2005,...already defined in *.obj",
    why?
    For example:
    //myclass.h
    DWORD dwMyData;
    class CMyClass
    {
    public:
    CMyClass(){}
    };
    //myotherclass.h
    #include "myclass.h"


    welcome to call me icq 35358130

  2. #2
    Join Date
    Apr 1999
    Location
    Chennai, INDIA
    Posts
    27

    Re: About Error LNK2005

    hi,
    If a global var is declared in the header file and if the header file is included in multiple CPP files, this error occurs. The global variable will be initialized in every CPP file. so during linking you'll get this error. To avoid this, you can put all the global variables in a seprate header file and include that header file wherever required.

    For example,

    #ifndef _GLOBAL_H_
    #define _GLOBAL_H_
    DWORD dwMyData;
    #endif // _GLOBAL_H_

    hope this solves your problem.

    Regards,
    Kalyan


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