Re: Register my own messages
On Mon, 21 Jan 2008 08:10:29 -0800 (PST), clinisbut <clinisbut@gmail.com>
wrote:
I'm trying to understand how to work with my own defined messages.
I've readed the essays from http://www.flounder.com/mvp_tips.htm which
I'm sure is very complet, is too technical for my level.
I found an example at http://www.codersource.net/mfc_user_event.html
but doesn't seems to work... I'm getting this error:
"int CODERSOURCEMSG" (?CODERSOURCEMSG@@3HA) already defined in
PostMessage01.obj
Debug/PostMessage01.exe : fatal error LNK1169: one or more multiply
defined symbols found
I defined int CODERSOURCEMSG in a header file appart. I don't
understand why vc is saying thats a duplicate declaration...
An object may be declared many times, but it can only be defined once. The
definition is what gives it storage and may contain an initializer, while
the declaration merely declares its type and name so that it can be used. A
definition is always a declaration, but the reverse doesn't necessarily
hold. It sounds like your header file contains a definition, so every file
that #includes the header defines this int, and because it isn't declared
static, the linker complains about multiple definitions. To fix it, use
extern in the header file:
extern const int UWM_MYMSG;
And in one .cpp file, write:
const int UWM_MYMSG = RegisterWindowMessage(...);
This defines UWM_MYMSG in one and only one place, but the declaration in
the header file allows anyone who #includes the header to use it. Since its
value will never change, I also declared it const.
--
Doug Harrison
Visual C++ MVP