c++ - Help Displaying a Vector In int main() -
the program i've written creates deck of card can shuffled , player takes 4 cards , puts them corresponding columns.
i can display single card doing this:
int main() { card card; cout << card << endl; }
the problem is, want display player class, made of vector of cards , should display 4 cards , put them 4 separate columns. using this:
int main() { deck deck; deck.shuffle(); player player; cout << player; }
does not display anything, in fact, gives me error.
how can display 4 cards in corresponding 4 columns?
also, here entire code have far, in case want go through yourself:
#include <iostream> #include <algorithm> #include <vector> #include <cstdlib> #include <ctime> using namespace std; enum suits { diamond, club, heart, spade }; class card { private: int rank; suits suit; public: card(); card(suits, int); int getrank() { return rank; } suits getsuit() { return suit; } void setrank(int rankvalue) { rank = rankvalue; } void setsuit(suits suitvalue) { suit = suitvalue; } }; ostream & operator<<(ostream &, card); card::card() { rank = 1; suit = spade; } card::card(suits suitvalue, int rankvalue) { rank = rankvalue; suit = suitvalue; } ostream & operator<<(ostream & out, card acard) { switch (int rank = acard.getrank()) { case 1: out << "ace"; break; case 11: out << "jack"; break; case 12: out << "queen"; break; case 13: out << "king"; break; default: out << rank; } switch (suits suit = acard.getsuit()) { case diamond: out << " of diamonds"; break; case spade: out << " of spades"; break; case heart: out << " of hearts"; break; case club: out << " of clubs"; break; } return out; } class randominteger { public: randominteger(); unsigned int operator() (unsigned int max); }; randominteger::randominteger() { srand(time(0)); } unsigned int randominteger::operator()(unsigned int max) { unsigned int rval = rand(); return rval % max; } randominteger randomizer; class deck { card cards[52]; int topcard; public: deck(); void shuffle(); bool isempty() { return topcard <= 0; } card draw(); }; extern randominteger randomizer; deck::deck() { topcard = 0; (int = 1; <= 13; i++) { card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i); cards[topcard++] = c1; cards[topcard++] = c2; cards[topcard++] = c3; cards[topcard++] = c4; } } card deck::draw() { if (!isempty()) return cards[--topcard]; else { card spadeace(spade, 1); return spadeace; } } void deck::shuffle() { random_shuffle(cards, cards+52, randomizer); } class player { public: player(); void print(); card draw(deck &); typedef vector<card> cards; vector<cards> column; }; player::player() { column.push_back(vector<card>()); column.push_back(vector<card>()); column.push_back(vector<card>()); column.push_back(vector<card>()); } card player::draw(deck & adeck) { (int = 0; < 4; i++) column[i].push_back(adeck.draw()); } void player::print() { cout << "col 1 \t col 2 \t col 3 \t col 4 \n"; bool more = true; (int j = 0; more; j++) { more = false; (int = 0; < 4; i++) if (j < column[i].size()) { cout << column[i][j] << "\t"; more = true; } else cout << "\t\t"; cout << endl; } } int main() { deck deck; deck.shuffle(); player player; while (!deck.isempty()); cout << player; }
sorry amateur question, know it's simple, , in advance.
cout << player;
this not work, because you've not overloaded operator<<
type player
. please first implement this:
ostream & operator<<(ostream &out, const player & player) { //your implementation }
if needs access private members, make friend
of player
.
i noticed class player
has function called print
, maybe call operator<<()
. if call it, suggest use following function signature:
void print(ostream &out) const { //use `out`, instead of `cout` now! }
and call operator<<
, as:
ostream & operator<<(ostream &out, const player & player) { player.print(out); return out; }
advantage of implementation can output console file also.
Comments
Post a Comment