PDA

View Full Version : Newbie Cocoa Question - Error Compiling




br-
Aug 10, 2006, 12:34 AM
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:
#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

#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:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}


Thanks.



mduser63
Aug 10, 2006, 12:37 AM
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.

robbieduncan
Aug 10, 2006, 12:45 AM
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:


- (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:


-(void) dealloc
{
[contents release];
[super dealloc];
}


I would recommend getting a book and reading it!

br-
Aug 10, 2006, 12:46 AM
Thanks guys. :)