james-collinss-macbook-prorog15 jamescollins$ gcc -framework Foundation AddressBook.m prog15.11.m -o prog15.11
AddressBook.m: In function -[AddressBook initWithName:]:
AddressBook.m:8: warning: conflicting types for -(id)initWithNameNSString *)name
AddressBook.h:11: warning: previous declaration of -(AddressBook *)initWithNameNSString *)name
AddressBook.m: In function -[AddressBook list]:
AddressBook.m:34: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:39: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:40: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:48: error: dealloc undeclared (first use in this function)
AddressBook.m:48: error: (Each undeclared identifier is reported only once
AddressBook.m:48: error: for each function it appears in.)
AddressBook.m:48: error: syntax error before { token
AddressBook.m: At top level:
AddressBook.m:53: warning: incomplete implementation of class AddressBook
AddressBook.m:53: warning: method definition for -dealloc not found
got these error messages when i tried to compile a program from a book on Objective-C.
-i was told cString is no longer used, so i understand that warning message.
the thing that i don't understand is AddressBook.m:48 error: 'dealloc' undeclared (first use in this function)...
i have in my interface file
i will include the interface file which i called AddressBook.h
and my implementation file which i called AddressBook.m
and my test file which i called prog15.11.m
didn't i declare dealloc in my interface file?
what is the syntax error before '{' token?
AddressBook.m: In function -[AddressBook initWithName:]:
AddressBook.m:8: warning: conflicting types for -(id)initWithNameNSString *)name
AddressBook.h:11: warning: previous declaration of -(AddressBook *)initWithNameNSString *)name
AddressBook.m: In function -[AddressBook list]:
AddressBook.m:34: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:39: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:40: warning: cString is deprecated (declared at /System/Library/Frameworks/Foundation.framework/Headers/NSString.h:345)
AddressBook.m:48: error: dealloc undeclared (first use in this function)
AddressBook.m:48: error: (Each undeclared identifier is reported only once
AddressBook.m:48: error: for each function it appears in.)
AddressBook.m:48: error: syntax error before { token
AddressBook.m: At top level:
AddressBook.m:53: warning: incomplete implementation of class AddressBook
AddressBook.m:53: warning: method definition for -dealloc not found
got these error messages when i tried to compile a program from a book on Objective-C.
-i was told cString is no longer used, so i understand that warning message.
the thing that i don't understand is AddressBook.m:48 error: 'dealloc' undeclared (first use in this function)...
i have in my interface file
Code:
-(void) dealloc;
i will include the interface file which i called AddressBook.h
Code:
#import <Foundation/NSArray.h>
#import <stdio.h>
#import "AddressCard.h"
@interface AddressBook: NSObject
{
NSString *bookName;
NSMutableArray *book;
}
-(AddressBook *) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(void) dealloc;
@end
and my implementation file which i called AddressBook.m
Code:
#import "AddressBook.h"
@implementation AddressBook;
// set up the AddressBook's name and an empty book
-(id) initWithName: (NSString *) name
{
self = [super init];
if (self) {
bookName = [[NSString alloc] initWithString: name];
book = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addCard: (AddressCard *) theCard
{
[book addObject: theCard];
}
-(int) entries
{
return [book count];
}
-(void) list
{
int i, elements;
AddressCard *theCard;
printf ("\n======== Contents of: %s =========\n",
[bookName cString]);
elements = [book count];
for ( i = 0; i < elements; ++i ) {
theCard = [book objectAtIndex: i];
printf ("%-20s %-32s\n", [[theCard name] cString],
[[theCard email] cString]);
}
printf("========================================\
============\n\n");
-(void) dealloc
{
[bookName release];
[book release];
[super dealloc];
}
@end
and my test file which i called prog15.11.m
Code:
#import "AddressBook.h"
#import <Foundation/NSAutoreleasePool.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *aName = @"Julia Kochan";
NSString *aEmail = @"jewls337@axlc.com";
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"tony.iannino@techfitness.com";
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"steve@kochan-wood.com";
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"jbaker@kochan-wood.com";
AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];
AddressBook *myBook = [AddressBook alloc];
// first set up four address cards
[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];
// now initialize the address book
myBook = [myBook initWithName: @"Linda's Address Book"];
printf ("Entries in address book after creation: %i\n",
[myBook entries]);
// Add some cards to the address book
[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
printf ("Entries in address book after adding cards: %i\n\n",
[myBook entries]);
// list all the entries in the book now
[myBook list];
[card1 release];
[card2 release];
[card3 release];
[card4 release];
[myBook release];
[pool release];
return 0;
}
didn't i declare dealloc in my interface file?
what is the syntax error before '{' token?