outflowboundary
outflowboundary

Reputation: 11

Create new class instance from within another instance method (Objective C)

I am (essentially) new to programming and attempting to learn Objective-C. I am working on a unit conversion program that uses an instance of a class to store and convert a unit. So for example, if I would like to convert 1 ft to inches, the program does the following:

  1. Create a new instance of Length class (myLength)
  2. Store the value and unit in a new instance of the class (myMass)
  3. Call an instance method to convert the value to new units

My Length class is working fine and just as I expect.

Now I would like to create a new class called Area that creates two instances of the Length class, and uses the Length class to perform the conversions. However, when I attempt to declare a new instance of my Length class from within Area.m, I get the warning "Length undeclared".

Is it possible to create an instance of a different class from within an instance method? More specifically, can I create two instances of my Length class from within the Area class instance method?

(Please forgive me in advance if I have botched any of my terminology).

Here's a snippet of the code in Area.m that is giving me trouble:

@implementation Area

-(void) setCompound: (double) myVal unit1: (char) c1 unit2: (char) c2

{

// Create two new instances of the Length class
// THE FOLLOWING TWO LINES ARE GIVING ME TROUBLE
Length * aLength = [[Length alloc] init];  
Length * bLength = [[Length alloc] init];

[aLength setLength:myVal units: c1]; // Set the value and units of first length

[bLength setLength: 1 units: c2]; // Set the value and units of the second length

}

@end

Upvotes: 1

Views: 327

Answers (1)

heisenberg
heisenberg

Reputation: 9759

You need to import your Length header file in Area.h

#import "Length.h"

Upvotes: 2

Related Questions