2011-11-01 51 views
1

之前*錯誤(預期說明符限定符列表錯誤),我有以下代碼:預計*在Xcode

@interface Building : GameObject 
{ 
    CGPoint mapSquarePos; 
    CGPoint SquareSize; 
    bool isBuilding; 
    UIImage *BuildingImage; 
    Person *garnisonedPerson; //Error is here 
} 

我已導入Person.h文件。這是什麼原因錯誤

+5

不Person.h進口Building.h? –

回答

4

嘗試

@class Person; 
@interface Building : GameObject 
{ 
    CGPoint mapSquarePos; 
    CGPoint SquareSize; 
    bool isBuilding; 
    UIImage *BuildingImage; 
    Person *garnisonedPerson; //Error is here 
} 

,並在實現文件

如果不起作用進口Person.h,似乎BuildingImage可能是一個類名。

更新Rickyman20

@class Person 

告訴編譯器的人是一類。通常我不會在頭文件中導入其他頭文件。我這樣做的一個例子是當我使用cocos2d。

在我的頭文件的情況下#import "cocos2d.h讓我的頭內使用整個的cocos2d庫,而不會導致編譯器錯誤

+2

允許變量名與類名相同(它不會導致編譯時錯誤),儘管實際上這樣做會是一個糟糕的主意(因爲它會破壞類名)。 –

+0

謝謝!有效。現在,它的工作,@class究竟做了什麼? – rdelfin

+0

我更新了關於@class的註釋。它讓編譯器知道Person是一個類而不是拼寫錯誤。在你的實現中導入Person.h使得你的類可用的所有方法。 (我知道當我忘記這麼做時,xcode不會自動完成我的自定義方法) –