Which header to include?

Started by
5 comments, last by dave 19 years, 4 months ago
I've got a class that's being used in two related programs. One of them uses MFC, and the other one is using DirectX without MFC. I'm having trouble figuring out what to include for the windows types. If I include <afx.h>, it will compile by itself, but when I try to build I get an error in my non-MFC program that says not to explicitly include windows.h in an MFC program (because I include windows.h in another file). If I include <windows.h>, the same thing happens in my MFC program. Is there a header somewhere that I can include with the windows types (HDC, BOOL, etc..) that will let me get around this? Another approach I tried, but had no success with, was to try something like: #ifdef USE_MFC #include <afx.h> #else #include <windows.h> #endif but this didn't work either. Any help is much appreciated!
Advertisement
Umm, im not up to scratch with using MFC, but, do you define around your own headers.

ie.


imaginary header.h
#ifndef _MYHEADER_FILE_HEADER_H_#define _MYHEADER_FILE_HEADER_H_/*... // header file code here*/#endif  // end this preprocessor directive


What this code does is first it checks to see if '_MYHEADER_FILE_HEADER_H_' has been registered in this program, ie, has this header file already been included. If not it defines the '_MYHEADER_FILE_HEADER_H_', then includes ur own header file, if it does exist it leaves the header.

Although this probably isnt the soluttion it is the only thing i can think of.

ace
Yep, I've already got all my headers set up with the #ifndef <etc etc etc> directives. But I *did* just go double check to make sure :P


I'm growing to dislike MFC more and more..
I've heard that MFC isnt appreciated by the majority of programmers.

There is nothing more educational than doing all of the work urself, even though the coding standard probs won't be as high as the mfc stuff.

I don't use it

ace
Okay, I fiddled around some, and my idea of using the preprocesser directives seems to be working for the MFC one. However, it's still not working properly for the non-mfc one.

In my class, I need to call LoadImage to load a bitmap from a resource. To do this, I need to get call AfxGetInstanceHandle, so I still need the afxwin.h header.

Through playing around, and a bit of googling, apparently the order in which I include the files matters. If I go:

#include <windows.h>
#include <afxwin.h>

I'll get the error I mentioned above, about not including windows.h in mfc applications.


I'm playing around a bit more now, but it's looking promising.. still open to suggestions if there's a nicer way of solving this

Hmm... now I'm getting some other issues (assertion fails) when I try to call AfxGetInstanceHandle.. for now, I'm just gonna comment that out, since I'm not actually loading my image from a resource... just thought it'd be good to leave that capability there.

In the meantime, I'll look for another way to get a handle to the instance besides the above mentioned function.
Seems strange. Maybe it'd be worth checking out the code for afx.h

It is incredibly unlikely but there might be something wrong in there. I'm thinking that maybe windows.h includes afx.h and since ur re-including it there is a conflict. But i honestly can't see this as the likely reason.

If you want, whack up ur code here, with
tags and i might be able to spot something.

ace

This topic is closed to new replies.

Advertisement