2012-12-23 35 views
2

我的運氣很可能是非常明顯的東西,從我身邊溜走了,但我一直在C2143掙扎了很長一段時間,我很難過。Vs2012編譯器錯誤C2143:丟失;之前*

game.h(21): error C2143: syntax error : missing ';' before '*' 
game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

Game.h

#ifndef GAME_H_ 
#define GAME_H_ 

#include <irrlicht.h> 
using namespace irr; 
using namespace irr::core; 
using namespace irr::scene; 
using namespace irr::video; 
using namespace irr::io; 
using namespace irr::gui; 

#include <iostream> 

#include "CInput.h" 
#include "CAssets.h" 
using namespace rtsirr; 

IrrlichtDevice *device = 0; 
IVideoDriver *driver = 0; 
ISceneManager *manager = 0; 
CAssets *assets = 0; // Line 21, error here 

#endif 

CAssets.h

#ifndef ASSETS_H_ 
#define ASSETS_H_ 

#include "Game.h" 

namespace rtsirr { 

class CAssets 
{ 
public: 
    CAssets(); 
    virtual ~CAssets(); 
    ITexture* getTexture(stringw name); 
    IMesh* getMesh(stringw name); 
    IAnimatedMesh* getAnimatedMesh(stringw name); 

    void load(); 

private: 
    map<stringw, ITexture *> *textures; 
    map<stringw, IMesh *> *meshes; 
    map<stringw, IAnimatedMesh *> *animatedMeshes; 
}; 

} 

#endif 

似乎CAssets沒有被識別爲有效的類型,但我想不出爲什麼。是什麼導致了這個問題?

謝謝。

+1

我可以問一個有點相關的問題嗎?在**頭文件***中,所有那些變量decl都在做什麼? – WhozCraig

+0

@WhozCraig好點!將它們更改爲_extern_。 – pajm

回答

3

您有循環依賴在您的包括Game.h包括CAssets.h,其在包括Game.h之前甚至包括CAssets。預處理器的結果會有所不同,具體取決於包含的順序。

從你的示例代碼看,Game.h並不需要知道太多關於CAssets以外的其他類型。你可以使用預先聲明替換CAssets.h包含:

class CAssets; 

你甚至可以提供一個CAssets_fwd.h,做只。否則,您將需要在這兩個頭文件之間打破循環依賴關係

+0

那麼,目標是任何包含Game.h的東西都可以訪問CAssets。你的方法仍然可以嗎?另外,我是否也需要在那裏提供名稱空間? – pajm

+0

@帕特里克莫里亞蒂:不,它不是。你應該尋找一種使'CAssets'獨立於'Game.h'的方法。是的,命名空間是聲明的一部分,所以當然應該在那裏...... –

相關問題