Tutorial :Can someone explain these errors to me?



Question:

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!


Solution:1

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...


Solution:2

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;


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »