Object C makes an integer from a pointer with no passing objects to pass as arguments

I am trying to make blackjack in a C lens and I have problems with passing objects. The My Hand class basically takes the deck and draws cards from it, adding them to the array.

Manual methods are used here:

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

Here is the problematic part of main:

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

The second line contains the error "integer from pointer without cast". Whenever I run the code, the hand never has any cards, because there is no deck for drawing (I think :)). Do I need to go through or disassemble the deck * differently? (if you need me to post more code just ask)

Thanks guys !: D



Change regarding generosity . Although I believe that the answer I accepted is well explained, and this question received an average of more than 17 views per day over the past year and a half. This means that this question is probably the first experience that many people encounter with stackoverflow , and improving it will not only show us the best light, it will probably save a lot of time for people. Although I have good answers now, I want to make sure that it is as perfect as possible, especially because we really do not know how many of these people were able to follow him and solve their problem.

Go ahead and make changes to existing answers or add your own. (Mods, do you think this will be a good candidate for the wiki community?)

+4
source share
3 answers

The problem, based on the additional code that you indicated below, is that you have two methods with the same name, but whose parameters do not match, namely - (id)init: (Deck*)deck and - (id)init: (int)newvalue .

This is usually not a problem, but in this case the types are structurally different - a pointer and an int. The compiler can distinguish what you mean based on the type of the recipient, but this only works when it has a static type. For example, if you have:

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

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

Since alloc returns id , not a Hand , he does not know that the init call in [[Hand alloc] init:deck] is Hand , not a Card . For more information on this, see Apple Docs on Static Typing .

The simplest (and smartest) solution is to rename the methods to indicate the type of argument. For example, you can use initWithCardValue: and initWithDeck:

EDIT: Also yes, listen to the suggestions of other messages about the correct behavior inside the init method. (This does not raise a warning, but may cause segfault.)

+12
source
  • As Phil Nash said in a comment on your question, make sure you import the headers for Hand and Deck into your main file. This should stop the warning (and it was a warning, not an error).

  • In your init method, remember to call [super init] , make sure it does not return nil and initializes the returned object. The most common way to do this is:

     if ((self = [super init])) { //Initialize here } return self; 
  • Declaring variables for arrays is not enough; you also need to create arrays and put them in variables. Only then can you put things in arrays.

+2
source

Have you called [super init] your Hand init: method:?

In addition, you add Maps to the map array; is it configured correctly?

+1
source

All Articles