i am new to Objective-C, not entirely sure where I am going wrong here.
I am trying to get the program to print the area and perimeter of a square.
The program is telling me that I am sending an undeclared identifier to the perimeter and area methods
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int width;
int height;
}
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int) width;
-(int) height;
-(int) area;
-(int) perimeter;
@end
@implementation Rectangle
-(void) setWidth:(int)w
{
width = w;
}
-(void) setHeight: (int)h
{
height = h;
}
-(int) width
{
return width;
}
-(int) height
{
return height;
}
-(int) area
{
return width*height;
}
-(int) perimeter
{
return (2*width + 2*height);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *rect1 = [[Rectangle alloc] init];
[rect1 setWidth:2];
[rect1 setHeight:7];
NSLog(@"The perimeter of rect1 is: %i and the area is: %i", area, area);
}
return 0;
}
There’s no variable named area, it is a method. Try
[rect1 area];and[rect1 perimeter];, you already created the object and used two methods correctly, you slipped on the area 🙁 Good luck!