C++ 在头文件中使用extern的全局对象

C++ 在头文件中使用extern的全局对象,c++,global,extern,C++,Global,Extern,我想要一个我在程序中创建的类的全局实例。 现在,我可以对从库(例如Qt)导入的整数、浮点或类执行相同的操作。 这是我的结构 文件:Common.h #ifndef COMMON_H #define COMMON_H #include "CChess.h" extern CChess game; #endif 文件:TestGame.cpp #include "common.h" #include "CChess.h" CChess game; int main(int argc,char **

我想要一个我在程序中创建的类的全局实例。 现在,我可以对从库(例如Qt)导入的整数、浮点或类执行相同的操作。 这是我的结构

文件:Common.h

#ifndef COMMON_H
#define COMMON_H
#include "CChess.h"
extern CChess game;
#endif
文件:TestGame.cpp

#include "common.h"
#include "CChess.h"
CChess game;
int main(int argc,char **argv)
{
   //main code
}
文件:CChess.h

#ifndef CCHESS_H
#define CCHESS_H


#include "Common.h"
#include "CBoard.h"

class CChess
{
public:
    CBoard mqGameBoard;
    PinchState current_hand_state;
    char mcPlayerTurn;

    //constructors
    CChess();
    ~CChess() {}
    //methods
    void setPinchState(PinchState current_hand_state);
    PinchState getPinchState();
    void Start();
    void GetNextMove(CPiece* qpaaBoard[8][8]) ;
    void AlternateTurn();
};

#endif
我得到:

Error   75  error C2146: syntax error : missing ';' before identifier 'game'    .\Common.h  
Error   76  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   .\Common.h  93
我能做什么?
同样的事情也适用于int、float等

问题在于include文件的循环依赖性:
Common.h
需要
CChess.h
需要
Common.h
等等


您需要以某种方式打破这个循环,比如在
CChess.h
中不包含
Common.h
,因为在那里似乎不需要它。

头文件
CChess.h
是什么样子的?“你没有忘记分号吗?”约阿希姆皮勒堡回答第二个问题。你赢得了水晶球奖。我刚刚在问题中添加了它。嗯,现在似乎需要它(你看不到它,但CChess.cpp使用一个共同声明为extern的变量。h.你能建议什么替代方案?@mbiks避免全局变量(这通常是一个很好的建议)?使用指针而不是非指针值(那么只需要类声明,而不需要完整的类定义)?无法避免全局变量,因为我使用OpenGL,所以Render、Idle和main之间的变量必须由所有3个变量看到