Newbie Cocoa Question - Error Compiling

Discussion in 'Mac Programming' started by br-, Aug 9, 2006.

  1. br-
    Expand Collapse
    macrumors member

    Joined:
    Aug 7, 2006
    #1
    Hey guys, I'm just starting out with Cocoa and I'm trying to make a basic GUI calculator app. My problem is that whenever I compile, I get an error saying:

    error: parse error before '=' token

    It's referring to line 5 of the CSController.h file. Here's all the code:

    CSController.h:
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface CSController : NSObject
    {
        NSMutableString *contents = [NSMutableString stringWithCapacity:10];
        IBOutlet NSTextField *textField;
        IBOutlet NSTextView *textView;
    }
    - (IBAction)clear:(id)sender;
    - (IBAction)divide:(id)sender;
    - (IBAction)dot:(id)sender;
    - (IBAction)eight:(id)sender;
    - (IBAction)enter:(id)sender;
    - (IBAction)equals:(id)sender;
    - (IBAction)five:(id)sender;
    - (IBAction)four:(id)sender;
    - (IBAction)minus:(id)sender;
    - (IBAction)multiply:(id)sender;
    - (IBAction)nine:(id)sender;
    - (IBAction)one:(id)sender;
    - (IBAction)plus:(id)sender;
    - (IBAction)seven:(id)sender;
    - (IBAction)six:(id)sender;
    - (IBAction)three:(id)sender;
    - (IBAction)two:(id)sender;
    - (IBAction)zero:(id)sender;
    
    @end
    
    CSController.m
    Code:
    #import "CSController.h"
    
    @implementation CSController
    
    - (IBAction)clear:(id)sender
    {
    }
    
    - (IBAction)divide:(id)sender
    {
    }
    
    - (IBAction)dot:(id)sender
    {
    }
    
    - (IBAction)eight:(id)sender
    {
    	[contents appendString:@"8"]
    	[textField setStringValue:contents]
    }
    
    - (IBAction)enter:(id)sender
    {
    }
    
    - (IBAction)equals:(id)sender
    {
    }
    
    - (IBAction)five:(id)sender
    {
    }
    
    - (IBAction)four:(id)sender
    {
    }
    
    - (IBAction)minus:(id)sender
    {
    }
    
    - (IBAction)multiply:(id)sender
    {
    }
    
    - (IBAction)nine:(id)sender
    {
    }
    
    - (IBAction)one:(id)sender
    {
    }
    
    - (IBAction)plus:(id)sender
    {
    }
    
    - (IBAction)seven:(id)sender
    {
    }
    
    - (IBAction)six:(id)sender
    {
    }
    
    - (IBAction)three:(id)sender
    {
    }
    
    - (IBAction)two:(id)sender
    {
    }
    
    - (IBAction)zero:(id)sender
    {
    }
    
    @end
    
    And my main.m which I haven't modified:
    Code:
    #import <Cocoa/Cocoa.h>
    
    int main(int argc, char *argv[])
    {
        return NSApplicationMain(argc,  (const char **) argv);
    }
    
    Thanks.
     
  2. mduser63
    Expand Collapse
    macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #2
    You can't initialize a variable/object (in this case 'contents') in the interface section. You only declare stuff in the interface. You can initialize things in the implementation section.
     
  3. robbieduncan
    Expand Collapse
    Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    To expand on what mduser63 is saying you would normally do your instance level initialisation in the init method (unless you are going to be woken from a nib file then you might want to do it in awakeFromNib).

    So for example:

    Code:
    - (id) init
    {
    if ([super init])
    {
    contents = [[NSMutableString stringWithCapacity:10] retain];
    return self;
    }
    return nil;
    }
    
    As we have retained the contents (no GC till Leopard!) we need to release it when the object dies:

    Code:
    -(void) dealloc
    {
    [contents release];
    [super dealloc];
    }
    
    I would recommend getting a book and reading it!
     
  4. br-
    Expand Collapse
    thread starter macrumors member

    Joined:
    Aug 7, 2006

Share This Page