Hello all,
I have posted many times questions about Java related topics. This would probably be one of my few C++ postings. The problem I am having is printing out the contents of a vector declared in a class, and inherited in other classes that need to access it. I suppose I want to know how to fix my problem, but first I need to know why the way I am doing what I am doing is not working at all. It prints nothing that I need. Here are my files
First the CardPile.cpp file. The .h files I won't include, as there is nothing extra in them that the cpp file won't show...at least I don't think there is.
#include "cardPile.h"
CardPile::CardPile()
{
vector<Card> theDeck;
}
void CardPile::addCard(Card scrd, vector<Card> destPl)
{
destPl.push_back(scrd);
}
void CardPile::listCards(vector<Card> lst)
{
cout << "This is a test" << endl;
for(int i=0; i < lst.size(); i++)
{
cout << lst[i].rank << " " << lst[i].suit;
}
}
Card CardPile::removeCard(Card crd, vector<Card> srcPl)
{
//special note here. in removing a card from the top of a pile, it should always be found
Card aCrd;
aCrd.suit = 'F'; //if the suit is F then the card wasn't found
for(int i=0; i < srcPl.size(); i++)
{
aCrd = srcPl[i];
if(aCrd == crd)
{
return aCrd; //found the card
}
}
return aCrd; //card not found to be removed
}
ostream & operator<< (ostream &ostr, const Card &rhs)
{
ostr << rhs.rank << rhs.suit << endl;
return ostr;
}
The next is my drawPile.cpp, DrawPile class inherits cardPile Class throug the public CardPile statement.
#include "cardTypes.h"
DrawPile::DrawPile()
{
cardsLeft = 52;
}
void DrawPile::initDeck(vector<Card> td) //set up the deck of cards
{
for(int i=0; i<13; i++)
{
//just trying to print the first 13 cards of one suit
//as chars
Card crd;
crd.suit = crd.sts[0];
crd.rank = crd.rnks[i];
}
}
void DrawPile::shuffle(vector<Card> deck) // shuffle the deck
{
}
int DrawPile::getCardNum() const //tells how many cards are left in the draw pile
{
return cardsLeft;
}
Last is the main.cpp file that will call into effect the listCards method from the DrawPile that inherited it from CardPile
#include "cardPile.h"
#include "cardTypes.h"
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
int main()
{
CardPile Deck;
DrawPile d;
d.initDeck(Deck.theDeck);
d.listCards(Deck.theDeck);
return 0;
}
and just in case the Card.cpp is like this for the Card Class
#include "cardPile.h"
Card::Card()
{
char sts[4] = {'S','C','H','D'};
char rnks[13] = {'1','2','3','4','5','6','7','8','9','T','J','Q','K'};
color_ = ' ';
suit = ' ';
rank = ' ';
}
char Card::getColor() const
{
return color_;
}
void Card::setColor(char &st)
{
if(st == 'S' || st == 'C')
{
color_ = 'b';
}
else if(st == 'H' || st == 'D')
{
color_ = 'w';
}
else
{
cout << "The char is currently " << st << endl;
cout << "Cannot determine color. Wrong character form." << endl;
}
}
bool Card::operator== (const Card &rhs)
{
return ((Card::suit == rhs.suit) && (Card::rank == rhs.rank));
}
These are the classes that are currently relevant to me trying to print out the values of the vector, but it doesn't work at all, or it prints garbage when I try various changes in the initDeck method. I don't know what I'm doing at all obviously, and if someone could please help me out I would much appreciate it.