2011-09-01 48 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; 

接口声明之前。

相关问题