help in overloading ostream op

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sahm

    #1

    help in overloading ostream op

    hi,
    i have to create a program that will print the 52 cards in a deck using
    the overloaded ostream operator. it worked fine....then i hve to create
    a function that will print the cards in hand in random(5, 10).

    //error it shld only have two parameters
    ostream& operator<<(ostr eam& out, vector<Card>& deck2, vector<Card>&
    hand)
    {

    for(unsigned int i = 0; i < hand.size(); i++)
    {
    int random;
    random = randNum(0, 51);

    //if the card is picked, don't add it in
    hand,instead add another card/s in the vector
    if(deck2[random].isPicked())
    {
    deck2[random].printCard(out) ;
    out << endl;
    int last = hand.size() - 1;
    hand.push_back( hand[last]);
    for(int i = last; i > deck2[random].isPicked();
    i--)
    {
    hand[last] = hand[last - 1];
    }

    }

    else
    {
    hand[i] = deck2[random];
    deck2[random].setPicked(true );
    hand[i].printCard(out) ;
    out << endl;
    }
    }
    return out;
    }

    what shld i put in the parameters of ostream& operator<<(..) in order
    to print the card in hand

  • Ron Natalie

    #2
    Re: help in overloading ostream op

    sahm wrote:[color=blue]
    > hi,
    > i have to create a program that will print the 52 cards in a deck using
    > the overloaded ostream operator. it worked fine....then i hve to create
    > a function that will print the cards in hand in random(5, 10).[/color]

    I think you need to define your problem better.
    As you noted operator<< should only have two operands.

    You should separate the generation of the hand from the deck
    from outputing a hand or a single card. I suggest the following.

    First write
    ostream << Card;

    Then using that also write
    ostream << vector<Card>

    Then right other functions that make the deck and hands.

    I would suggest it would be easier if you actually removed the
    cards from the deck after it is picked rather than maintaining
    some sort of picked state. Think about the real world you're
    attempting to model here.
    [color=blue]
    >[/color]

    Comment

    Working...