c++visual-studiocompiler-errors

Compiler Error for unknown reasons (C++)


I get a compiler error (C2061) in MSVS C++ 2010 IDE, when I try to compile. I simply got no clue as to why it occurs, as I can see no apparant error.

#pragma once
#ifndef _CCOMPONENT_H
#define _CCOMPONENT_H

#include "CGame.h"

class CComponent
{
public:
    CComponent(CGame &game);
    ~CComponent();

protected:
    virtual void Draw();
    virtual void Update();
    virtual void Init();
    void Dispose();
};


#endif // _CCOMPONENT_H

Compiler Error:

 ccomponent.h(10): error C2061: syntax error : identifier 'CGame'

Contents of CGame.h

/**************************************************
* 
*
****************************************************/
#pragma once
#ifndef _CGAME_H
#define _CGAME_H

#include <cstdio>
#include <list>
//#include <Box2D/Box2D.h>
#include <allegro5\allegro5.h>
#include "SharedDef.h"

#include "CComponent.h"
#include "CTestPlayer.h"

using namespace std;

class CComponent;
class CTestPlayer;

const int MAX_COMPONENTS = 255;

class CGame
{
public:
    // CONSTRUCTORS
    CGame();
    ~CGame();

    // ACCESSORS
    ALLEGRO_DISPLAY *GetGameDisplay();
    ALLEGRO_EVENT_QUEUE *GetGameEventQueue();

    list<CComponent> GetComponents() { return *m_Components; }
    void AddComponent(CComponent component);

    bool IsRun();
    void StartTimer();
    void StopTimer();

    virtual bool ShouldDraw();
    virtual bool ShouldUpdate();
    virtual void Update(void);
    virtual void Draw(void);
    virtual void Dispose();

protected: 
    virtual void RegisterEventSources();
    virtual void Initialize();

private: 
    bool InitializeAllegro();

private:
    ALLEGRO_DISPLAY *m_Display;
    ALLEGRO_EVENT_QUEUE *m_EventQueue;
    ALLEGRO_TIMER *m_Timer;
    ALLEGRO_EVENT m_Event;

    list<CComponent> *m_Components;
    //CComponent *m_Components[MAX_COMPONENTS];

    bool m_bIsRunning;
    bool m_bShouldDraw;
};

#endif // _CGAME_H

and, "class CGame" is declared and defined in "CGame.h", so I really cannot see why....


Solution

  • Your headers try to #include each other - you can't do that. Remove the #include of the game header from the component header and use a forward declaration of CGame instead.

     CComponent(class CGame &game);
    

    On a design point, problems like this usually mean you have got something wrong. I think it unlikely that components should know about the game they are part of.