Amazon.com Widgets Error in creating subclass in Program 8.7 - ClassB
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 10:05:58 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 8
| | |-+  Error in creating subclass in Program 8.7 - ClassB
Pages: [1]   Go Down
Print
Author Topic: Error in creating subclass in Program 8.7 - ClassB  (Read 141 times)
todrico
Newbie
*
Posts: 17


Email




« on: March 14, 2013, 02:29:28 PM »

 Huh
I am getting an error in the interface file for ClassB. It says "Cannot find interface declaration for 'ClassA', superclass of 'ClassB'.  I have gone over and over the examples in the book of program 8.6 and 8.7 to make sure the interface files and implementation files for ClassA and ClassB are correct. I did move the print to ClassA. Need another set of eyes, please!


Quote
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface ClassA : NSObject
{
    int x;
}

-(void) initVar;
-(void) printVar;

@end
« Last Edit: March 14, 2013, 02:41:25 PM by todrico » Logged
todrico
Newbie
*
Posts: 17


Email




« Reply #1 on: March 14, 2013, 02:37:10 PM »

Sorry, I punched the post before I was finished pasting my code.  Let me try again. The error occurs at "@interface ClassB: ClassA". Another error occurs in ClassB implementation file when I set "x = 200;" The error I get is "Use of undeclared identifier 'x'". It is probably something simple but I cannot find it.


Quote
#import <Foundation/Foundation.h>

@interface ClassA : NSObject
{
    int x;
}

-(void) initVar;
-(void) printVar;

@end


Quote
#import "ClassA.h"

@implementation ClassA

-(void) initVar
{
    x = 100;
}

-(void) printVar
{
    printf("x = %i", x);
}
@end


Quote
#import <Foundation/Foundation.h>

@interface ClassB: ClassA

-(void) initVar;

@end


Quote
#import "ClassB.h"

@implementation ClassB

-(void) initVar
{
    x = 200;
}

@end
Logged
todrico
Newbie
*
Posts: 17


Email




« Reply #2 on: March 15, 2013, 08:35:15 AM »

I found the solution to my own problem.  When you create the ClassB.h and Class.m files through "File, New File" the window where you declare the subclass, I selected NSObject out of habit.  When I selected "ClassA" for the subclass, everything worked.  However, there was no mention in the book to make sure the proper parent class, ClassA, is selected when creating the files. Being new to Xcode this would have been very helpful a poor assumption on the author's part.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.