views:

5970

answers:

3

For more info on fix this error, see here

I'm trying to make blackjack in objective C, and am having trouble passing objects around. My Hand class basically takes a deck and draws cards from it, adding them to an array.

Here's the Hand methods involved:

- (id)init : (Deck*) deck
{
    [self draw: deck];
    [self draw: deck];
    return self;
}

- (void)draw: (Deck*)deck;
{
    Card* C= [deck drawFromDeck];
    [cards addObject: C];
}

Here's the problematic part of main:

Deck* deck=[[Deck alloc] init];
Hand* hand=[[Hand alloc] init: deck ];

The second line of that gets the "integer from pointer without a cast" error. Whenever I run the code, the hand never has cards in it because there's no deck to draw from (I think :) ). Do I need to pass or parse the Deck* differently? (if you need me to post any more code, just ask)

Thanks guys!! :D

A: 

Did you call [super init] in your Hand's init: method?

Also, you are adding Cards to a cards array; is that being set up correctly?

Abizern
I looks like he didn't, and while he probably should, that wouldn't give him this error in that location.
Jason Coco
+1  A: 
  1. As Phil Nash said in his comment on your question, make sure you have imported the headers for both Hand and Deck into your main file. That should stop the warning (and it was a warning, not an error).

  2. In your init method(s), don't forget to call [super init], check that it did not return nil, and initialize the object that it did return. The most common way to do that is:

    if ((self = [super init])) {
        //Initialize here
    }
    return self;
    
  3. Declaring variables for the arrays isn't enough; you also have to create the arrays and put them in the variables. Only then can you put things into the arrays.

Peter Hosey
+3  A: 

The problem, based on the extra code you've posted below, is that you have two methods with the same name but whose parameters don't match, namely - (id)init: (Deck*)deck and - (id)init: (int)newvalue.

Normally this isn't a problem, but in this case the types are structurally different - a pointer and an int. The compiler can distinguish which you mean based on the type of the receiver, but this only works when it has its static type. For example, if you had:

Hand *h = [Hand alloc];
h = [h init: deck];

It would stop giving you a warning. This is very unusual code, though - alloc and init almost always go on the same line.

Since alloc returns an id, and not a Hand it doesn't know that the init call in [[Hand alloc] init:deck] is to a Hand, and not a Card. See Apple's Docs on static typing for more information about that.

The easiest (and a reasonable) solution is to rename the methods to indicate the type of the argument. For example, you could use initWithCardValue: and initWithDeck:.

EDIT: Also, yes, heed the suggestions from the other posts about the proper behavior inside the init method. (It's not causing the warning, but it may be causing the segfault.)

Jesse Rusak