Hi sorry for yet another circular inclusion problem but I need help understanding why I cant add a new class CSprite to my project that inherits CShape. I get an error C2504 base class is undefined. I've tried so many permutations of including files. Also can anyone suggest a neater/efficient/correct way of how my includes should be set up. (include-graph)
Here are the header files with the functions snipped:

Main.h -->

#ifndef main_h
#define main_h

// Windows and DirectX includes
#define STRICT 
#include "windows.h"
#include <d3d9.h>
#include <d3dx9.h>
//#include <vector>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// Application centered includes
#include "D3DDefs.h"
#include "CApplication.h"

using namespace std;

// GLOBALS
class CApplication; 
extern CApplication   g_App;        // Controls all the DirectX objects and devices

#endif // main_h *********************************************************

CApplication.h -->

    #ifndef CApplication_h
    #define CApplication_h

    #include "Main.h"
    #include "CSpriteManager.h"
    #include "CShape.h"
    #include "CSprite.h"
    #include "CLight.h"

    class CSpriteManager;
    class CShape;
    class CSprite;
    class CLight;

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

#endif // CApplication.h *************************************************

CSpriteManager.h -->

#ifndef CSpriteManager_h
#define CSpriteManager_h

#include "Main.h"
#include "CShape.h"
#include "CSprite.h"

class CShape;
class CSprite;

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

    int Clone(CShape* pSource, CShape* pDest);
    int Clone(CSprite* pSource, CSprite* pDest);

};// CSpriteManager

#endif // CSpriteManager_h

CShape.h -->

#ifndef CShape_h
#define CShape_h

#include "Main.h"       

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

}; // CShape

#endif // CShape_h

CSprite.h -->

#ifndef CSprite_h
#define CSprite_h

#include "CShape.h"

//class CShape;

class CSprite: public CShape
{
public:
    CSprite(void);
    ~CSprite(void);

};// CSprite

#endif // CSprite_h

All the other classes compile fine its just when I added this class as inherited from CShape.
I look forward to your thoughts :)

Header files can be quite confusing and with the inclusion of so many header files, it can be difficult to track the bug.

A possible culprit is with main.h and CApplication.h

main.h

#include "CApplication.h
[...]

CApplicationn.h

#include "main.h"
[...]

CApplication.h <--- expanded

[substitute in main.h]
[...]
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
// Application centered includes
#include "D3DDefs.h"
#include "CApplication.h"     <-- a Big NO NO, causes a circular reference
[...]

Try to avoid including a header file that contains the file itself.

There are several practices you should follow when working with header files.

  1. Avoid including a header file (.h) in another header file. Use a class declaration instead. This works as long as you declare pointers or references. Try to use pointers and references to those classes, but if you can't, you have to include the header file.

Like in your example,

#ifndef CSpriteManager_h
#define CSpriteManager_h
#include "Main.h"
//#include "CShape.h"    <--- remove these header files
//#include "CSprite.h"

class CShape;    <--- use class declarations if you can
class CSprite;

class CSpriteManager
{
public:
    CSpriteManager(void);
    ~CSpriteManager(void);
    int Clone(CShape* pSource, CShape* pDest);
    int Clone(CSprite* pSource, CSprite* pDest);
};// CSpriteManager
#endif // CSpriteManager_h
  1. Following the first point, put the header files (.h) in the source file (.cpp) instead.

  2. If you're going to include a header file inside a header (.h) make sure it does not contain the file itself. Refer back to the first example, where CApplication.h includes itself after the main.h substitution.

I have done exactly as you said and it works!!! hooray! roll-on the next headache!

thankyou

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.