The following code are my include files:
However, I find that when I use #include <Windows.h>
, the project can not compile, it will come with an error:
"LoadImageA": is not a member of "ImageData"
On this line:
im.LoadImage("bg.png");
However, if I do not use #include `, everything works fine. Can any one help me?
This is a common problem with the Win32 API, and affects any symbols that have the same name as Win32 API functions that support
TCHAR
data.In this case,
Windows.h
includeswinuser.h
, which defines the Win32 APILoadImage()
function as follows:The
#define
statements are what is causing your problem. A#define
is global in scope, and doesn't respect file boundaries, namespaces, class definitions, etc. It is just a straight text replacement. So, while your code is calling this:The preprocessor changes it to the following, which is what the compiler sees:
Or:
Depending on whether you are compiling your project for MBCS or Unicode (in this case, you are using MBCS).
If Microsoft would be nicer to C++ developers, the Win32 API headers would use inline functions instead of C macros, eg:
That would allow proper scoping and overloading of any names repeated across libraries, classes, etc. But alas, Microsoft seems unwilling to do that after all these years :( The Win32 API is primarily designed for C and other C-compatible languages, it provides very little provisions for C++ (not counting things like GDI+, COM, etc).
In any case, for your situation, the simplest solution is to either:
rename the
ImageData.LoadImage()
method to a more unique name.use an
#undef LoadImage
statement (assuming you don't need to use the actual Win32 APILoadImage()
function anywhere else in your source file):Or, if you want to be a little more restrictive:
LoadImage is a macro inside
Windows.h
. It will rewrite any and all occurences of that text to LoadImageW or LoadImageA. Hence your problems. You need to choose different name for your stuff or#undef
it temporarily.