PDA

View Full Version : Why am I gtting this error message




HyperSnake
Feb 23, 2009, 01:27 PM
Hi I'm writing my very first program with a GUI right now, a simple calculator app. I have fixed all issues the compiler came with except that after the #import I get an error message like this:

error: syntax error before '=' token
error: syntax error before '=' token
error: syntax error before '=' token
error: syntax error before '=' token

Here is my @interface:

#import <Cocoa/Cocoa.h>

@interface BECalculator : NSObject {
IBOutlet id textField;
NSString *theOutput;
double firstNumber = 0;
double secondNumber = 0;
double theResult;
}
- (IBAction)add:(id)sender;
- (IBAction)divide:(id)sender;
- (IBAction)equals:(id)sender;
- (IBAction)multiply:(id)sender;
- (IBAction)setFirstNumberTo1:(id)sender;
- (IBAction)setFirstNumberTo2:(id)sender;
- (IBAction)setFirstNumberTo3:(id)sender;
- (IBAction)setFirstNumberTo4:(id)sender;
- (IBAction)setFirstNumberTo5:(id)sender;
- (IBAction)setFirstNumberTo6:(id)sender;
- (IBAction)setFirstNumberTo7:(id)sender;
- (IBAction)setFirstNumberTo8:(id)sender;
- (IBAction)setFirstNumberTo9:(id)sender;
- (IBAction)subtract:(id)sender;
@end


Here is the @implementation:
#import "BECalculator.h"

//It is here I get the error message.

@implementation BECalculator

- (IBAction)add:(id)sender
{
theOutput = [NSString stringWithString: @"+"];
}

- (IBAction)divide:(id)sender
{
theOutput = [NSString stringWithString: @"/"];
}

- (IBAction)equals:(id)sender
{
if (theOutput == @"+")
theResult = firstNumber + secondNumber;
else if (theOutput == @"-")
theResult = firstNumber - secondNumber;
else if (theOutput == @"*")
theResult = firstNumber * secondNumber;
else if (theOutput == @"/")
theResult = firstNumber / secondNumber;

[textField setDoubleValue: theResult];
}

- (IBAction)multiply:(id)sender
{
theOutput = [NSString stringWithString: @"*"];
}

- (IBAction)setFirstNumberTo1:(id)sender
{
if (firstNumber =! 0)
secondNumber = 1;
else
firstNumber = 1;
}

- (IBAction)setFirstNumberTo2:(id)sender
{
if (firstNumber =! 0)
secondNumber = 2;
else
firstNumber = 2;
}

- (IBAction)setFirstNumberTo3:(id)sender
{
if (firstNumber =! 0)
secondNumber = 3;
else
firstNumber = 3;
}

- (IBAction)setFirstNumberTo4:(id)sender
{
if (firstNumber =! 0)
secondNumber = 4;
else
firstNumber = 4;
}

- (IBAction)setFirstNumberTo5:(id)sender
{
if (firstNumber =! 0)
secondNumber = 5;
else
firstNumber = 5;
}

- (IBAction)setFirstNumberTo6:(id)sender
{
if (firstNumber =! 0)
secondNumber = 6;
else
firstNumber = 6;
}

- (IBAction)setFirstNumberTo7:(id)sender
{
if (firstNumber =! 0)
secondNumber = 7;
else
firstNumber = 7;
}

- (IBAction)setFirstNumberTo8:(id)sender
{
if (firstNumber =! 0)
secondNumber = 8;
else
firstNumber = 8;
}

- (IBAction)setFirstNumberTo9:(id)sender
{
if (firstNumber =! 0)
secondNumber = 9;
else
firstNumber = 9;
}

- (IBAction)subtract:(id)sender
{
theOutput = [NSString stringWithString: @"-"];
}
@end



Thank you in advance,

Ben.



szymczyk
Feb 23, 2009, 01:38 PM
@interface BECalculator : NSObject {
IBOutlet id textField;
NSString *theOutput;
double firstNumber = 0;
double secondNumber = 0;
double theResult;
}





You're giving firstNumber and secondNumber initial values in the interface, which is illegal in Objective C. The following fix should eliminate the error:

double firstNumber;
double secondNumber;

Set firstNumber and secondNumber to 0 in an initialization method.

HyperSnake
Feb 23, 2009, 01:57 PM
You're giving firstNumber and secondNumber initial values in the interface, which is illegal in Objective C. The following fix should eliminate the error:

Set firstNumber and secondNumber to 0 in an initialization method.

Thank you, the error messages are indeed gone. How do I put an initialization method into a GUI program. I don't see how I do it when not using main() and without affecting the if statements.

caveman_uk
Feb 23, 2009, 02:06 PM
Also you can't compare strings the way you're doing it as when you do an '=' comparison on an object you're comparing the object addresses not the object contents.

szymczyk
Feb 23, 2009, 02:54 PM
How do I put an initialization method into a GUI program.

NSObject has an init method that you override in your BECalculator class. Here's an example of what you would write.

- (id)init
{
self = [super init];
if (self != nil) {
// initialization code
firstNumber = 0.0;
secondNumber = 0.0;
}
return self;
}

Put that code in your implementation file, BECalculator.m.

HyperSnake
Feb 23, 2009, 02:57 PM
NSObject has an init method that you override in your BECalculator class. Here's an example of what you would write.

- (id)init
{
self = [super init];
if (self != nil) {
// initialization code
firstNumber = 0.0;
secondNumber = 0.0;
}
return self;
}

Put that code in your implementation file, BECalculator.m.

Thank you I will try it out

@caveman_uk I changed it to the isEqualToString method