views:

229

answers:

3

I wish c++, was a little more specific on the messages they give. The following error is being thrown in the document below

main.h

#ifndef main_h
#define main_h

//includes
#include    <windows.h>
#include    <commctrl.h>
#include    <d3d9.h>
#include    <fstream>

#include "capplication.h"

//constants
#define TITLE "D3D Tut 01: Create Window"
#define WINDOW_X 350
#define WINDOW_Y 320

//Button ID's
#define ID_START 1
#define ID_CANCEL 2

//globals
extern CApplication g_App;

//function prototypes
LRESULT CALLBACK        WindowProcedure(HWND,UINT,WPARAM,LPARAM);

#endif

The only header file that could possible throw this error is the capplication.h. given below

capplication.h

#ifndef capplication_h
#define capplication_h

#include"main.h"

class CApplication
{
public:
                        CApplication(void);
                        ~CApplication(void);

    void                InitWindow(void);
    void                SaveSettings(void);
    void                LoadSettings(void);

    void                KillWindow(void);

    inline bool         GetWindowStatus(void)
                        {
                            return m_bRunningWindow;
                        }

    inline HWND         GetWindowHandle(void)
                        {
                            return m_hWindow;
                        }

    inline void         SetWindowStatus(bool bRunningWindow)
                        {
                            m_bRunningWindow = bRunningWindow;
                        }
private:
    bool                m_bRunningWindow;

    HWND    m_hWindow,
            m_hBtnStart,
            m_hBtnCancel,
            m_hLblResolution,
            m_hCbResolution,
            m_hLblBackBuffer,
            m_hCbBackBuffer,
            m_hLblDepthStencil,
            m_hCbDepthStencil,
            m_hLblVertexProcessing,
            m_hCbVertexProcessing,
            m_hLblMultiSampling,
            m_hCbMultiSampling,
            m_hLblAnisotropy,
            m_hCbAnisotropy;

    DWORD   m_dwWidth,
            m_dwHeight,
            m_dwVertexProcessing,
            m_dwAnisotropy;

    D3DFORMAT           m_ColorFormat,
                        m_DepthStencilFormat;

    D3DMULTISAMPLE_TYPE m_MultiSampling;
};

#endif

Besides that, the only suspicious thing I see is fstream given in the first code. I did have it as fstream.h

But VC++ was not recognizing it so I was told to remove the h and I did. now I am down to this error. and I have no clue what it could be. Possibly something obvious

+2  A: 

You have a circular inclusion problem. main.h includes capplication.h, and then capplication.h includes main.h. Remember that in C++, inclusion is a literal copy-and-paste of the contents of the included header. What is happening is that when main.h includes capplication.h, the include statement that brings main.h in again happens before the delcaration of the CApplication class, and so when main.h is processed for the second time, the line extern CApplication g_App is reached before the declaration of the CApplication class is.

It doesn't really make sense for a capplication.h to include main.h, so you should move anything that capplication.h needs out from main.h into a separate header, and then have both main.h and capplication.h include that header.

For future reference, the message "syntax error: missing ';' before identifier" usually means that the type you are trying to declare the variable in question as has not been defined.

Tyler McHenry
That makes sense. But when I do that, it catches errors within capplication.h which comes before the original error i was having. This is not my code . I am actually trying to do a tutorial at http://www.two-kings.de/tutorials/dxgraphics/dxgraphics01.html. it also has a circular inclusion in its code.
numerical25
It's bad style, yes, that the headers depend on each other, but they each have include guards. If `main.h` is included, then `main_h` is defined, `capplication.h` is included, `capplication_h` is defined, `main.h` is included, but `main_h` is defined so the PP goes back to `capplication.h`, then to `main.h`. It appears to me that the definition of class `CApplication` would be above `extern CApplication g_App;` in the translation unit.
Daniel Trebbien
+2  A: 
extern CApplication g_App;  // missing ‘;’ before identifier ‘g_App’

What could possibly go wrong with this line? The error message itself isn't particularly self-explanatory, but with only a little experience, it's clear that the likely culprit is that it doesn't know what CApplication is at this point.

How could this happen? I expect Visual Studio has an option to display preprocessor output, but you can do it in your head too. If the .cpp source file includes main.h, what text gets included? (The text of main.h, and within it the text of capplication.h.) If the source file includes capplication.h, what text gets included? (The blank line at the top of capplication.h, then the text of main.h, then the rest of capplication.h with the definition of CApplication. What went wrong?)

Does main.h really need to #include capplication.h or would a declaration of the class suffice for the purposes of declaring g_App?

// in main.h
class CApplication;
extern CApplication g_App;
John Marshall
A: 

Your main.h does not need to include capplication.h in order to declare extern CApplication g_App. You could try:

#ifndef main_h
#define main_h

//includes
#include    <windows.h>
#include    <commctrl.h>
#include    <d3d9.h>
#include    <fstream>

//constants
#define TITLE "D3D Tut 01: Create Window"
#define WINDOW_X 350
#define WINDOW_Y 320

//Button ID's
#define ID_START 1
#define ID_CANCEL 2

//globals
class CApplication;
extern CApplication g_App;

//function prototypes
LRESULT CALLBACK        WindowProcedure(HWND,UINT,WPARAM,LPARAM);

#endif
Daniel Trebbien