Thread: MFC include BS

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    MFC include BS

    Ok I've been coding in C++ for a long time. My project is getting rather large and not-withstanding the MSVC crash every 50 builds it is going rather well.

    However the include stuff is a gigantic mess. So I went to edit some of it and now I have well over 1250 errors and no way to fix any of it.

    In a C++ program correct me if I'm wrong but all headers needed for the CPP file should be in the respective header.

    But in MFC the frieking includes are in the damned CPP file? Why?
    And now I have a dialog (CLayersDlg.h) that needs to include CMainFrm.h, but the CMainFrm.h must also include CLayersDlg.h.
    But when I do this, the stupid compiler tells me that basically this is undefined:

    Code:
    #include "CLayersDlg.h"
    ...
    CLayersDlg  LayersDlg;
    The solution in C++ is to use a #ifndef #define #endif block. This will prevent multiple includes but will solve the issue when you have a cyclical include. For some reason MFC is not doing this. Even though it defines some god awful #if (!defined AFX_SOME_BIG_NUMBER_BLAH_BLAH_BLAH) it is still using the same principle.

    So why does this not work?

    MainFrm.h
    Code:
    #if !defined(AFX_MAINFRM_H__F76A4064_27DD_4BB7_A85E_0FD7771C3730__INCLUDED_)
    #define AFX_MAINFRM_H__F76A4064_27DD_4BB7_A85E_0FD7771C3730__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    
    #include <io.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <vector>
    
    //Core include
    #include "ZeldaEditor.h"
    
    
    //User includes
    
    #include "LayersDlg.h"          //Active layers dialog
    
    
    //#include "MapProject.h"         //Project->New response dialog
    //#include "Tiles.h"              //Tile tool dialog(s)
    //#include "CResourceFile.h"      //Resource file class
    //#include "TileManager.h"        //Tile manager class
    
    
    
    class CMainFrame : public CFrameWnd
    {
    	
    protected: // create from serialization only
    	CMainFrame();
    	DECLARE_DYNCREATE(CMainFrame)
    
    // Attributes
    public:
    
    // Operations
    public:
      //friend class CZeldaEditorDoc;
      bool bProjectReady;
      bool bResourceFileLoaded;
      
      //Active layers dialog
      LayersDlg   dlgLayers;
      
    ....
    LayersDlg.cpp
    D:\VC6Projects\ZeldaEditor2\MainFrm.h(57) : error C2146: syntax error : missing ';' before identifier 'dlgLayers'
    D:\VC6Projects\ZeldaEditor2\MainFrm.h(57) : error C2501: 'LayersDlg' : missing storage-class or type specifiers
    D:\VC6Projects\ZeldaEditor2\MainFrm.h(57) : error C2501: 'dlgLayers' : missing storage-class or type specifiers
    And for one thing to compile LayersDlg.cpp and flag an error in MainFrm.h is just friggin stupid. MainFrm.h was included long before this and worked just fine.

    What the heck is going on and how can I fix this include hell?

    I need the CMainFrame class quite often for my dialogs so I need to include it so I can do this:

    CMainFrame *ptrFrame=(CMainFrame *)AfxGetMainWnd();
    ptrFrame->DoSomethingInCMainFrameClass();

    I don't want a pointer to the actual MFC class, but I want a pointer to my derived class which is why I need to include CMainFrm.h

    All this is really making me...um...mad.

    And since my project is over 4K lines......I'm really mad now that I tried to add one thing and the whole program came crashing to its knees. Same issue. Included a header file so I could use it's class data type in a class header file and it said the damned thing was undefined. Unbelievable. I know what it is doing but I don't know why. It is saying that it is undefined because even though I put #include<some_header>, some_header has already been included and so it doesn't include it. But if some_header has already been defined then some_header inside of <some_header.h> should also be already defined. So doing this should work:

    SomeObject.h
    Code:
    #ifndef SOMEOBJECT
    #define SOMEOBJECT
    
    class SomeObject
    {
    };
    
    
    #endif
    SomeClass.h
    Code:
    #ifndef SOMECLASS
    #define SOMECLASS
    
    #include "SomeObject.h"
    
    class SomeClass
    {
      SomeObject   Object;
     ...
    };
    This should work. But this is exactly what I'm doing with LayersDlg and it is not working.
    Last edited by VirtualAce; 10-29-2005 at 03:49 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I fixed it by just dumping the pre-compiled headers. Somehow the PCH file got messed up and was causing lots of issues. Bad MS, bad.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I don't see why precompiled headers are all that necessary to be enabled by default, we've left the days of 50 MHz 486's behind, most compilation jobs take a few minutes at best and all PCH does is shave a few seconds off. It's not like I've ever heard of programmers being tied to the clock like call centre peeps ("Compile this in 35 seconds or you're fired!")...

    But yes Bubba, if you use MFC in a big program you are gonna have to deal with little problems like that. Call it a learning experience.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Oh and any suggestions on serialization? My editor saves and loads tile maps along with tile resource files (former BMPs, header removed and replaced with my own pertinent info, and RLE compressed RGB data). The streaming method for MFC doesn't really fit my needs so I wrote my own save file functions. Bad practice? Should I always use Serialize()?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you only really need to use serialize() if you want to serialize MFC objects. Otherwise, if you are just writing/reading your own non-MFC objects you can do your own thing outsize CArchive and CFile.


    In a C++ program correct me if I'm wrong but all headers needed for the CPP file should be in the respective header.
    It's a matter of programming style, I've seen it done both ways by various programmers. and VC++ 6.0/7.0 application wizards do it both ways too -- stdafx.h contains all commonly used header files, which *.cpp files contain includes for the files that are not so common throught the program. And you can feel free to add additional stuff to stdafx.h. Just make sure you do a full rebuild so that the precompiled headers get rebuilt.

    Precompiled headers are not unique to Microsoft compilers. The last time I was on GNU.org there was some comments about g++ also using them. I don't know how wide-spread that idea has become.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  3. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. what do I have to include for mfc app
    By awarden in forum Windows Programming
    Replies: 4
    Last Post: 05-25-2002, 06:45 AM