Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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:
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

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
Thank you, thank you , thank you :)

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

MadDoc,
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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).
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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 :)
 

MadDoc

macrumors 6502
Original poster
Apr 25, 2005
329
5
UK
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 :)

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

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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

macrumors 68000
Feb 1, 2005
1,887
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.