开发者

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

/**************************************************
* 
*
******************************开发者_JAVA百科**********************/
#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....


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.


It looks like CGame.h is being compiled first. So imagine the steps

  1. CGame.h starts being compiled
  2. CComponent.h gets included
  3. CComponent.h starts compiling due to being included
  4. CGame.h is included, but is skipped due to #pragma once (already started in step 1)
  5. Error: CGame is undefined symbol

Solution: Get rid of includes, replace with forward declarations. You forward declare CComponent in CGame.h, do the opposite in CComponent.h (forward declare CGame)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜