PDA

View Full Version : Objective-C: Where is the build error??




MadDoc
Oct 17, 2007, 07:40 AM
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?

// 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: &#37;f\n", [deskCalc accumulator]);
}

Really frustrating as I am sure it is only a really small thing!

Thanks,

MadDoc



kainjow
Oct 17, 2007, 07:59 AM
The problem comes from your comments.

You have several lines that are like this:
// ---------- Program section ---------- \\

A \ at the end of a line means it will continue until the next line.

From the official (http://gcc.gnu.org/onlinedocs/gcc-3.2.3/cpp/Initial-processing.html) GCC documentation:
A continued line is a line which ends with a backslash, \. The backslash is removed and the following line is joined with the current one. No space is inserted, so you may split a line anywhere, even in the middle of a word.

So remove the backslashes and it compiles :)

MadDoc
Oct 17, 2007, 08:12 AM
Thank you, thank you , thank you :)

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

MadDoc,

kainjow
Oct 17, 2007, 08:16 AM
I don't think I would have ever figured that out!

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).

Spike099
Oct 17, 2007, 03:24 PM
Very interesting to know. Nice find.

Soulstorm
Oct 18, 2007, 03:05 AM
#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 :)

MadDoc
Oct 18, 2007, 07:54 AM
#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 :)

What does that mean? Sorry if I'm a little slow (only just starting out!). I assume you are referring to using NSObject?

MadDoc,

mduser63
Oct 18, 2007, 08:38 AM
What does that mean? Sorry if I'm a little slow (only just starting out!). I assume you are referring to using NSObject?

MadDoc,

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.

Soulstorm
Oct 18, 2007, 10:51 AM
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.