John Smith
John Smith

Reputation: 633

The method is undefined for the type

I'm currently trying to implement a card game called 500.

Right here, I'm trying to index every type of bid possible into a simple index number. I've already created the enums for the suits in another class. The problem I have right now is that I keep on getting an error

"The method Bid(int, Card.Suit) is undefined for the type Bid".

I dont understand why this is happening. Any help would be appreciated.

public Bid(int pTricks, Suit pSuit)
{
    assert pTricks >= 6;
    assert pTricks <= 10;
    this.trickCount = pTricks;
    this.trumpSuit = pSuit;
}


public Bid(int pIndex)
{

    switch (pIndex) {
    case 0: Bid(6, Suit.SPADES);
    case 1: Bid(6, Suit.CLUBS);
    case 2: Bid(6, Suit.DIAMONDS);
    case 3: Bid(6, Suit.HEARTS);
    case 4: Bid(6, null);
    case 5: Bid(7, Suit.SPADES);
    case 6: Bid(7, Suit.CLUBS);
    case 7: Bid(7, Suit.DIAMONDS);
    case 8: Bid(7, Suit.HEARTS);
    case 9: Bid(7, null);
    case 10: Bid(8, Suit.SPADES);
    case 11: Bid(8, Suit.CLUBS);
    case 12: Bid(8, Suit.DIAMONDS);
    case 13: Bid(8, Suit.HEARTS);
    case 14: Bid(8, null);
    case 15: Bid(9, Suit.SPADES);
    case 16: Bid(9, Suit.CLUBS);
    case 17: Bid(9, Suit.DIAMONDS);
    case 18: Bid(9, Suit.HEARTS);
    case 19: Bid(9, null);
    case 20: Bid(10, Suit.SPADES);
    case 21: Bid(10, Suit.CLUBS);
    case 22: Bid(10, Suit.DIAMONDS);
    case 23: Bid(10, Suit.HEARTS);
    case 24: Bid(10, null);

    }


}

Upvotes: 0

Views: 8504

Answers (3)

Andy
Andy

Reputation: 1994

  1. "The method Bid(int, Card.Suit) is undefined for the type Bid" That's right! There is no method with that signature - Bid(int, Card.Suit) is a constructor. (I can tell, because of the missing return type or keyword void between the access modifier and the name)

  2. To invoke a constructor from another one, you use the keyword this instead of the class name. AND the call to another constructor (or super constructor) must be the first statement, thus a switch as you coded it won't work.

3. You probably want to use a static initializer to create a map of Bids, e.g. (untested):

private static Map<Integer, Bid> bids = new HashMap<Integer, Bid>();

{
    bids.put(0, new Bid(6, Suit.SPADES));
    // ...
}

and then access the map:

public Bid getBidByIndex(int pIndex) {
    return bids.get(pIndex);
}

Upvotes: 1

Anna
Anna

Reputation: 303

Use a static initializer:

public static Bid create(int pIndex) {
    switch (pIndex) {
    case 0: return Bid(6, Suit.SPADES);
    case 1: return Bid(6, Suit.CLUBS);
    ...
}

Upvotes: 2

Jeremy Roman
Jeremy Roman

Reputation: 16355

You want to use the this keyword to invoke the other constructor.

e.g.

case 24: this(10, Suit.HEARTS); break;

Also, make sure to have break; at the end of each case.

Edit: This doesn't work as indicated in comments below. Move this logic into a private method that you call from the constructors, or use a static factory method to replace the second constructor.

Upvotes: 5

Related Questions