I was compiling one of the projects I work with, this time with VS2010, just to find out that one of the includes in windows.h has a typedef INPUT which was clashing with an export const string in the code I already have.
//winuser.h (line: 5332)
typedef struct tagINPUT {
DWORD type;
union
{
MOUSEINPUT mi;
KEYBDINPUT ki;
HARDWAREINPUT hi;
};
} INPUT, *PINPUT, FAR* LPINPUT;
//foo.h
//stuff here
extern FOO_DLL_API const string INPUT;
Now, I don’t use INPUT in the offending .cpp (and I do not own most of the code), and trying to minimize the impact, I did the following:
//myfile.cpp
#include <foo.h>
namespace windowsLib { //I added this
# include <windows.h>
}
using namespace windowsLib;
So far, this approach is working fine, but I wanted to ask you if you see potential problems with this approach, or if you have a better suggestion.
Edit:
I appreciate all the comments and explanations on why this is a bad idea. What I get from your comments is that I should change foo.h and put the contents into a namespace. However, by doing that, I would be affecting dozens of files, and some more, which will now require namespace qualifications.
Is there a way to do this “the right way” without touching all those files?
If I was the owner of the code, I would do this change and be done with it, but I have to propose a solution and get it approved and assigned to someone, etc. So it will be easier if the change is minimal.
Edit 2:
My final proposal was to split the class in two as follows:
//stub.cpp
#include <windows.h>
//Implementation of wrapper methods
//stub.h
class stub {
//public wrapper methods
}
//myfile.cpp
#include <stub.h>
#include <foo.h>
I’m accepting Benlitz answer since that suggestion would also solve the problem with the current minimal impact constrains I currently face. However, I thank you all for your comments.
It is a bad idea as explained in other answers. Here is an idea that might help you fix your issue:
//myfile.cpp
This might work because INPUT is an extern variable in foo.h, so neither the compiler nor the linker would care about it as long as you don’t use it in myfile.cpp. UnusedSymbol is a dummy name, you can write whatever name is not used in your source.