0

I am new to programming and is in the process of learning objective-c from Big Nerd Ranch. Using x-code, I am trying to create a class. Upon creating the class, X-Code is not recognizing it in main file. I created a new project, then created a new file ensuring the correct target was selected. When I try to type triangle, it says "Use of undeclared identifier." What am I doing wrong? Please help.

This is my header file.

#import <Foundation/Foundation.h>

@interface Triangle : NSObject
{
float lengthSideA;
float lengthSideB;
float lengthSideC;
}

@property float lengthSideA;
@property float lengthSideB;
@property float lengthSideC;

-(float) area;
-(float) perimeter;
-(float) hypothenuse;

@end

And this is my implementation file.

#import "Triangle.h"

@implementation Triangle

@synthesize lengthSideA, lengthSideC, lengthSideB;


-(float) perimeter
{
float a = [self lengthSideA];
float b = [self lengthSideB];
float c = [self lengthSideC];

return a + b + c;
}

-(float) area
{
float a = [self lengthSideA];
float b = [self lengthSideB];

return b * a / 2;
}

-(float) hypothenuse
{
float a = [self lengthSideA];
float b = [self lengthSideB];

return sqrt(a * a + b * b);
}

@end
2
  • 2
    Post the exact error message and the line of code within which it occurs. Commented Dec 9, 2013 at 15:13
  • With new compilers you don't need to declare ivars. In addition, you don't need to call the property getter [self lengthSideA] from inside the class, you can use directly lengthSideA or _lengthSideA for auto-generated ivars. Commented Dec 9, 2013 at 15:25

2 Answers 2

4

Add #import "Triangle.h" in the class where you are using your Triangle class. You have to import any external class before you can use it.

Sign up to request clarification or add additional context in comments.

Comments

2

First of all check if the file is imported. If it is imported, go to build settings and check if the header of the file is added to the compile sources. Also check if the file is added to the main bundle properly. I hope you selected the tick option which says add the reference of the file while adding the file. Hope it helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.