Objective-C: Where is the build error??

Discussion in 'Mac Programming' started by MadDoc, Oct 17, 2007.

  1. MadDoc macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #1
    Hi,

    I am just getting started in ObjC but I can't get the following program to build (2 build errors in Xcode) but I don't see anything wrong with the code. What am I missing?

    Code:
    // Implement a calculator class
    
    #import <objc/Object.h>
    #import <stdio.h>
    
    // ---------- Interface section ---------- \\
    @interface Calculator: Object
    {
    	double accumulator; // stores the current total
    }
    
    // Accumulator methods
    -(void) setAccumulator: (double) value;
    -(void) clear;
    -(double) accumulator;
    
    // Arithmetic methods
    -(void) add: (double) value;
    -(void) subtract: (double) value;
    -(void) multiply: (double) value;
    -(void) divide: (double) value;
    @end
    
    // ---------- Implementation section ---------- \\
    @implementation Calculator;
    // Accumulator methods
    -(void) setAccumulator: (double) value
    {
    	accumulator = value;
    }
    
    -(void) clear
    {
    	accumulator = 0;
    }
    
    -(double) accumulator
    {
    	return accumulator;
    }
    
    // Arithmetic methods
    -(void) add: (double) value
    {
    	accumulator += value;
    }
    
    -(void) subtract: (double) value
    {
    	accumulator -= value;
    }
    
    -(void) multiply: (double) value
    {
    	accumulator *= value;
    }
    
    -(void)divide: (double) value
    {
    	accumulator /= value;
    }
    @end
    
    // ---------- Program section ---------- \\
    int main (int argc, char *argv[])
    {
    	// Create a new calculator
    	Calculator *deskCalc;
    	deskCalc = [[Calculator alloc] init];
    	
    	// Clear the calculator
    	[deskCalc clear];
    	
    	// Set the accumulator to 100
    	[deskCalc setAccumulator:100];
    	
    	// Add 200
    	[deskCalc add: 200];
    	
    	// Print the result
    	printf("\nThe value of the accumulator is: %f\n", [deskCalc accumulator]);
    }
    Really frustrating as I am sure it is only a really small thing!

    Thanks,

    MadDoc
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    The problem comes from your comments.

    You have several lines that are like this:
    Code:
    // ---------- Program section ---------- \\
    A \ at the end of a line means it will continue until the next line.

    From the official GCC documentation:
    So remove the backslashes and it compiles :)
     
  3. MadDoc thread starter macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #3
    Thank you, thank you , thank you :)

    I don't think I would have ever figured that out!

    MadDoc,
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Me neither, but I opened it in TextMate, which colored the line after your comments as a comment, so then I knew there was something weird happening. Xcode doesn't work the same way, unfortunately (hopefully fixed in v3).
     
  5. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
  6. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    Code:
    #import <objc/Object.h>
    wow... I never thought I would see this again. It will not be too long until you decide to abandon Object as your basic object class :)
     
  7. MadDoc thread starter macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #7
    What does that mean? Sorry if I'm a little slow (only just starting out!). I assume you are referring to using NSObject?

    MadDoc,
     
  8. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #8
    He is, but don't worry, you're doing fine. IIRC, it looks like you're working through Programming in Objective-C. You'll get to using NSObject later on in the book.
     
  9. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #9
    Yes, I am referring to NSObject, as mduser63 said. But don't worry, you won't need to use it. That book you are probably using, Programming in Objective C is what got me started into OS X development. Hehe, I can assure you it's a great book.
     

Share This Page