473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with Vector Errors :(

Hi there,

I am trying to create card game, that shuffles the cads in deck

Draws hands and sorts........I am really struck here.....I get all compile errors with Vector<>

Any help would be rally great.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <rando.h>
  5.  
  6. class CardHand {
  7. public:
  8.  
  9.     CardHand(int initMaxCards);    
  10.     void AddCard(Card theCard);    
  11.     void SwapCards(int cardNum1,int cardNum2);
  12.  
  13.  
  14. private:
  15.       vector <Card> theCards;
  16.     int numCards;
  17.     int maxCards; };
  18.  
  19. class Card {
  20.   public:
  21.     Card();
  22.     void Assign(char r, char s);
  23.   private:
  24.     char rank;
  25.     char suit;
  26. };
  27.  
  28.  
  29. class DeckOfCards {
  30.   public:
  31.     DeckOfCards();
  32.     void Shuffle();
  33.     Card DrawFromTop();
  34.     bool IsEmpty();
  35.   private:
  36.     vector<Card> cards;
  37.     int numCards;
  38. };
  39.  
  40.  
  41.  
  42.  
  43. CardHand::CardHand(int initMaxCards):theCards(initMaxCards)
  44. {
  45.     maxCards = initMaxCards;
  46.     numCards = 0;        
  47. }
  48.  
  49.  
  50. void CardHand::SwapCards(int cardNum1, int cardNum2)
  51. {
  52.     if (((cardNum1 > 0) && (cardNum1 <= numCards)) &&
  53.         ((cardNum2 > 0) && (cardNum2 <= numCards)))
  54.     {
  55.         Card temp;
  56.         temp = theCards[cardNum1 - 1];
  57.         theCards[cardNum1 - 1] = theCards[cardNum2 - 1];
  58.         theCards[cardNum2 - 1] = temp;
  59.     }
  60. }
  61.  
  62. Card::Card()
  63. {
  64.     rank = '?';
  65.     suit = '?';
  66. }
  67.  
  68. void Card::Assign(char newRank, char newSuit)
  69. {
  70.     rank = newRank;
  71.     suit = newSuit;
  72. }
  73.  
  74.  
  75. /////////////////////////////////////////////////////////////
  76.  
  77.  
  78. DeckOfCards::DeckOfCards():cards(52)
  79. {
  80.    numCards = 0;
  81.    string suits = "SCHD";
  82.    string ranks = "23456789TJQKA";
  83.  
  84.    int suitNum, rankNum;
  85.    for (suitNum = 0; suitNum < suits.length(); suitNum++) {
  86.       for (rankNum = 0; rankNum < ranks.length(); rankNum++) {
  87.      cards[numCards].Assign(ranks[rankNum], suits[suitNum]);
  88.      numCards++;
  89.       }
  90.    }
  91. }
  92.  
  93.  
  94. void DeckOfCards::Shuffle()
  95. {
  96.    RandGen rando;
  97.    for (int i = 0; i < numCards; i++) {
  98.       int randPos = rando.RandInt(i,numCards-1);
  99.       Card temp = cards[i];
  100.       cards[i] = cards[randPos];
  101.       cards[randPos] = temp;
  102.    }
  103. }
  104.  
  105.  
  106. Card DeckOfCards::DrawFromTop()
  107. {
  108.    numCards--;
  109.    return cards[numCards];
  110. }
  111.  
  112.  
  113. bool DeckOfCards::IsEmpty()
  114.  
  115. {
  116.    return (numCards == 0);
  117. }
  118.  
  119.  
  120. void main(){
  121. DeckOfCards():cards(52) d1;
  122. d1.Shuffle();
  123. cout << "cards shuffled"<< endl;
  124.  
  125. }
Jan 29 '08 #1
2 1322
hsn
237 100+
my advice to you is to break down your code and try to find your error your self.
Jan 29 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Where's your using namespace std; ???
Jan 29 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dark Knight | last post by:
Hello, I am working on a program to get use to the vector.h Templete class. Program: #include <string> #include <iostream> #include <fstream> #include <vector>
1
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.