user100051
user100051

Reputation:

Can someone explain these errors to me?

Im getting these weird errors, but I dont understand them. Here are the errors:

error: variable - sized object may not be initialized (#1)

error: statically allocated instance of Objective-C class 'Joke' (#1)

error: statically allocated instance of Objective-C class 'Joke' (#1)

error: cannot convert to a pointer type (# 2)

(Note: The number after the error will indicate where the error was in my implementation file)

Here is my .m file:

#import "Joke.h"


@implementation Joke
@synthesize joke;
@synthesize rating;


- (id)init {
[super init];
return self;
}

- (void)dealloc {
[joke release];
[super dealloc];    
}

+ (id)jokeWithValue:(NSString *)joke {
Joke j = [[Joke alloc] init]; // (# 1) This is where #1 errors occurred
j.joke = joke;
return [j autorelease]; // (# 2) This is where #2 errors occurred
 }

@synthesize joke;
@synthesize rating;

@end

Thanks!

Upvotes: 2

Views: 5596

Answers (2)

xtophyr
xtophyr

Reputation: 184

You need a "*" before your variables -- for instance, "Joke *j = [[Joke alloc] init];"

You also only want @synthesize in there once - not for each property. Like this: @synthesize joke, rating;

Upvotes: 6

Jason Coco
Jason Coco

Reputation: 78393

Instances of Objective-C objects must be pointers, which is causing your problem. Your initialization of joke should be:

Joke *j = [[Joke alloc] init];

Also, it's a bad idea for an object to hold onto itself as a circular reference. You would have infinite recursion with j->joke->joke->joke->joke->joke...

Upvotes: 10

Related Questions