You need to include the <algorithm> header.Originally Posted by joenching
You need to include the <algorithm> header.Originally Posted by joenching
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
<algorithm> is the required header. As for which page tells you that...
http://cppreference.com/
This one apparently doesn't document the algorithm header.
http://www.sgi.com/tech/stl/
This one is ONLY about the STL.
http://msdn.microsoft.com/library/default.asp
One of the most complete ones, but it's a pain to navigate with anything but IE (as if it was impossible to implement a proper tree view in Gecko ...), quite slow and some people don't like it.
http://gcc.gnu.org/onlinedocs/libstd...3.4/index.html
Fast, but I don't know how complete or good. In any way, it seems the class list freaks out in my firefox and wanders off the screen.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I have already settled the second problem
just put #include <algorithm> will fix it.
For the first error, can anyone suggest me how i should do?
change Card& to intOriginally Posted by joenching
Or change the card member of Deck to vector<Card>. This probably makes more sense.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thantos:
After i remodifed the program, it can be compiler, however, it has a warning message:
I wanna show you my Card class, I think there should has some interact with card class.Code:int Deck::operator[] (int index) { if ( index >= 0 && index < 52 ) return card[index]; } warning C4715: 'Deck::operator[]' : not all control paths return a value
Here is my card.h
here is card.cppCode:#include <iostream> using namespace std; class Card { public: enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }; enum Suit { SPADES, HEARTS, CLUBS, DIAMONDS }; Card(); // Default constructor will initialize to Ace of Spades Card( Rank newRank, Suit newSuit ); Rank getRank() const {return rank;} Suit getSuit() const {return suit;} void setRank( Rank newRank ); void setSuit( Suit newSuit ); private: Rank rank; Suit suit; }; ostream& operator<<( ostream& os, const Card& card );
appericate itCode:#include <cassert> #include <iostream> #include "card.h" using namespace std; Card::Card() : rank( ACE ), suit( SPADES ) { } Card::Card( Rank newRank, Suit newSuit ) : rank( newRank ), suit( newSuit ) { } void Card::setRank( Rank newRank ) { rank = newRank; } void Card::setSuit( Suit newSuit ) { suit = newSuit; } ostream& operator<<( ostream& os, const Card& card ) { switch( card.getRank() ) { case Card::ACE: os << "Ace"; break; case Card::TWO: os << "Two"; break; case Card::THREE: os << "Three"; break; case Card::FOUR: os << "Four"; break; case Card::FIVE: os << "Five"; break; case Card::SIX: os << "Six"; break; case Card::SEVEN: os << "Seven"; break; case Card::EIGHT: os << "Eight"; break; case Card::NINE: os << "Nine"; break; case Card::TEN: os << "Ten"; break; case Card::JACK: os << "Jack"; break; case Card::QUEEN: os << "Queen"; break; case Card::KING: os << "King"; break; default: assert( 0 ); } os << " of "; switch( card.getSuit() ) { case Card::SPADES: os << "Spades"; break; case Card::HEARTS: os << "Hearts"; break; case Card::CLUBS: os << "Clubs"; break; case Card::DIAMONDS: os << "Diamonds"; break; default: assert( 0 ); } return os; }
CornedBee
I modified it to that:
it's almost work, but it still shows a error msg which is same as above when i try to compile it.Code:vector<Card> card; // STL vector of type card.
You discared Thantos' comment about doing error handling without ever doing anything like it, and now you're surprised it doesn't work?
Actually, I'd ditch the range test altogether and use vector::at to access the card, that does the range test and the error handling for you.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Doesn't at() throw an exception? If so they might not want to have their program crash just because they gave an invalid rangeOriginally Posted by CornedBee
You mean i need to do the error handling?
Well considering you'll have to do something if they give you an index out of range, yeah you'll have to do some error handling. Either as the class developer or as the class user
It means there has 52 cards in the Deck class
if i want to show the 53 card in that class.
The deck class will throw an error exception?
Currently it will simply crash. If you use at(), it will throw a std::out_of_range.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law