What's going on with “expected specifier-qualifier-list” error

独自空忆成欢 提交于 2019-12-01 09:27:40

问题


It is my GameEngine.h:

#import <Foundation/Foundation.h>
#import "GameArray.h";


@interface GameEngine : NSObject {
    GameArray *gameButtonsArray;
}

@property (nonatomic, retain) GameArray *gameButtonsArray;

And this is my GameArray.h:

#import <Foundation/Foundation.h>
#import "MyAppDelegate.h";

@interface GameArray : NSObject {
    NSMutableArray *gameButtonsArray;

}
@property (nonatomic, retain) NSMutableArray *gameButtonsArray;

It keep prompt my "expected specifier-qualifier-list" error i my GameEngine.h, and error said that "expected specifier-qualifier-list before 'GameArray'", what's going on?


回答1:


This is the best practice.

GameEngine.h

#import <Foundation/Foundation.h>

@class GameArray;

@interface GameEngine : NSObject {
    GameArray *gameButtonsArray;
}

@property (nonatomic, retain) GameArray *gameButtonsArray;

Then in GameEngine.m

#import "GameEngine.h"
#import "GameArray.h"

@implementation GameEngine    
//...
@end

This prevents circular references wherein one header imports a second header which imports the first which imports the second and so on in an endless cycle.




回答2:


Lose the semi-colon on line 2 in your .h file




回答3:


If removing the unnecessary semicolons does not fix your problem, most likely MyAppDelegate.h imports GameEngine.h creating a circular dependency between the GameEngine.h and GameArray.h. Try removing the #import "GameArray.h" from GameEngine.h and replacing it with:

@class GameArray;

Also add

#import "GameArray.h"

to GameEngine.m below the import of GameEngine.h



来源:https://stackoverflow.com/questions/2616334/whats-going-on-with-expected-specifier-qualifier-list-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!