Click to See Complete Forum and Search --> : About Error LNK2005


lry
May 4th, 1999, 10:51 PM
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

pkraman
May 5th, 1999, 12:27 AM
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