Why am I gtting this error message

Discussion in 'Mac Programming' started by HyperSnake, Feb 23, 2009.

  1. macrumors member

    Joined:
    Jan 5, 2009
    Messages:
    74
    Location:
    Switzerland
    #1
    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:

    Code:
    #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:
    Code:
    #import "BECalculator.h"
    
    [COLOR="SeaGreen"]//It is here I get the error message.[/COLOR]
    
    @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.
     
  2. macrumors regular

    Joined:
    Mar 5, 2006
    Messages:
    146
    #2
    You're giving firstNumber and secondNumber initial values in the interface, which is illegal in Objective C. The following fix should eliminate the error:

    Code:
    double firstNumber;
    double secondNumber;
    Set firstNumber and secondNumber to 0 in an initialization method.
     
  3. macrumors member

    Joined:
    Jan 5, 2009
    Messages:
    74
    Location:
    Switzerland
    #3
    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.
     
  4. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Messages:
    2,392
    Location:
    Hitchin, Herts, UK
    #4
    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.
     
  5. macrumors regular

    Joined:
    Mar 5, 2006
    Messages:
    146
    #5
    NSObject has an init method that you override in your BECalculator class. Here's an example of what you would write.

    Code:
    - (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.
     
  6. macrumors member

    Joined:
    Jan 5, 2009
    Messages:
    74
    Location:
    Switzerland
    #6
    Thank you I will try it out

    @caveman_uk I changed it to the isEqualToString method
     

Share This Page