views:

30

answers:

2

I have a programme I which I want to implement button class. I have declared all my variable in button.h and defined all methods in button.cpp and I am calling these functions in WINMAIN the following error appears.

keylogger.obj : error LNK2005: "struct HBITMAP__ * hOldBmp" (?hOldBmp@@3PAUHBITMAP__@@A) already defined in Button.obj

The error is for multiple defination hOldBmp but It is only defined in button.h

A: 

"Only defined in button.h" is exactly your problem. Unless you declared it as extern there and put the definition into a C++ source file (not header file), every translation unit will get their own definition of the variable.

Timo Geusch
A: 

Seems like a common error: you include the implementation of this hOldBmp pointer from two .obj files, so from two cpp files. So both obj files contain code to implement this pointer. The linker cannot decide which implementation to use in the final binary.

Solution: leave only the declaration in the header file. You may declare it extern or make it a static member variable of the button class. Put the definition in the cpp file.

xtofl