2011-09-01 47 views
0

有什麼不對下面的代碼? Xcode 4是說,帶有「Question」的兩個方法聲明由於「Question」之前的「expected」)而不能編譯,如下面代碼中的註釋所示:Question類編譯並且此代碼。以前已經工作,我做了一些修改的問題,而是支持他們去揣摩這個編譯時錯誤編譯器錯誤 - iOS設備的「預期‘)問題’之前」

#import <Foundation/Foundation.h> 
#import "Question.h" 

@interface AppState : NSObject { 

    int chosenAnswer; 
    int correctAnswers; 
    int currentQuestionNumber; 

    // this will contain the hash table of question objects 
    NSMutableDictionary *questionHash; 

} 

@property (nonatomic) int chosenAnswer; 
@property (nonatomic) int correctAnswers; 
@property (nonatomic) int currentQuestionNumber; 
@property (nonatomic, retain) NSDictionary *questionHash; 

- (void)  printQuestions; 
- (void)  printDescription; 
- (void)  addQuestion: (Question *) question; // <==== error 
- (int)   numberOfQuestions; 
- (void)  saveState; 
- (void)  resetState; 
- (Question *) currentQuestion; // <===== error 


@end 

這裏的Question.h:?

#import <Foundation/Foundation.h> 

#import "AppState.h" 

@interface Question : NSObject { 

    NSString *questionTxt; 
    int   correctAnswer; 
    int   number; 

    // this will contain the hash table of questions_answer objects 
    NSMutableDictionary *answerHash; 

} 


@property (nonatomic, retain) NSString * questionTxt; 
@property (nonatomic) int  correctAnswer; 
@property (nonatomic) int  number; 
@property (nonatomic, retain) NSMutableDictionary *answerHash; 


-(void)       addAnswer: (NSString *) answer; 
- (NSMutableArray *)    answerArray; 
- (void)    printDescription; 
- (void)    printAnswers; 
- (NSString *)   correctAnswerText; 
- (Question *)      currentQuestion; 

@end 
+2

你可以張貼Question.h? – Maz

+0

是的,我懷疑問題其實不是一種類型,或者是奇怪的東西。 – jtbandes

回答

4

循環依賴屆時AppState是進口問題和問題是進口屆時AppState。

向前宣佈他們中的一個打破週期,例如使用@class問題您屆時AppState @interface語句之前,像這樣

@class Question; 

@interface AppState : NSObject { 

    int chosenAnswer; 
    int correctAnswers; 
    int currentQuestionNumber; 

    // this will contain the hash table of question objects 
    NSMutableDictionary *questionHash; 
} 
... 

相關問題:@class vs. #import

+0

謝謝。這是問題所在。我現在更清楚@class所用的東西。 –

0

當你在問題#IMPORT 「AppState.h」 你犯了一個循環依賴。 最好將#import「AppState.h」和#import「Question.h」移動到實現部分。在頭見好就收

@class Question; 

@class AppState; 

接口聲明之前。

相關問題