jonnie
jonnie

Reputation: 12710

Expected identifier or '(' before @interface

First post and I am really hoping this is not a repetitive or solved question. I tried searching here and Google and while I have found similar Expected identifier or '(' errors none of the solutions work for me.

Basically I'm trying to learn Design patterns and as I used to know a bit of java I am trying to use it as an opportunity to learn objective-c so I have a java program that works and an xCode project that I get the error Expected identifier or '(' in my header file just before the @interface

this is my java solution (very simple I know):

public class Duck {

public void quack(){
        System.out.print("Quack!");

    }
    public void swim(){
        System.out.print("swimming duck!");
    }
public void display(){
    quack();
    swim();
        }

    }
public class mainClass {
    public static void main(String[] args){
        Duck duck = new Duck();
        duck.display();
    }
}

and this is my objective-c version.

//duck.h
#include <CoreFoundation/CoreFoundation.h>

@interface Duck : NSObject{ //Expected identifier or '('

}
@end

//  Duck.m
#include "Duck.h" 
@implementation Duck
-(void)quack{
    printf("Quack!");
}
-(void)swim{
    printf("swimming duck!");
}

-(void)display{
    [self quack];
    [self swim];    
}
@end

//  main.c
#include <CoreFoundation/CoreFoundation.h>
#include "Duck.m"

int main(int argc, const char * argv[])
{
    Duck *duck = [[Duck alloc] init];
    [duck display];
    return 0;
}

If any one can help I would greatly appreciate it, and again sorry if this is a duplicate post

Upvotes: 4

Views: 2308

Answers (4)

dans3itz
dans3itz

Reputation: 1615

//duck.h
//#include <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h> // or Cocoa/Cocoa.h

@interface Duck : NSObject//{ //Expected identifier or '('

//} not necessary if there are no instance fields
- (void)quack;
- (void)swim;
- (void)display;
@end

//  Duck.m
//#include "Duck.h"
#import "Duck.h" 
@implementation Duck
-(void)quack{
    printf("Quack!");
}
-(void)swim{
    printf("swimming duck!");
}

-(void)display{
    [self quack];
    [self swim];    
}
@end

//  main.c SHOULD BE ~main.m~ if using ObjC!!!
//#include <CoreFoundation/CoreFoundation.h>
//#include "Duck.m"
#import "Duck.h"

Additionally, get in to the habit of using NSString literals; @"example" for if/and when you decide to advance into Cocoa. Good luck with your studies.

Upvotes: 1

Dan Lister
Dan Lister

Reputation: 2583

It could be that you don't really need curly brackets on your empty interface:

@interface Duck : NSObject
@end

Upvotes: 0

GoldenJoe
GoldenJoe

Reputation: 8012

Try using import instead of include. Also, make sure that the CoreFoundation framework is actually part of your project.

Upvotes: -1

trojanfoe
trojanfoe

Reputation: 122468

The compiler doesn't know what NSObject is. If you look at the reference, you'll see that it's part of the Foundation framework, not CoreFoundation, so:

#import <Foundation/Foundation.h>

instead of:

#import <CoreFoundation/CoreFoundation.h>

Upvotes: 6

Related Questions