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.

When coding I recommend your write and test only a couple lines, or at most a single function, at a time. Since CardPile seems to be the first thing you wrote I'd start there. Given the constructor you posted I suspect you want CardPile to have a member that is declared as vector<Card> theDeck, in which case you declare the vector in CardPile.h, not in the constructor. In addCard you would just pass the card you want to add, and don't need to pass the cardPile. addCard is a member function so it will only be used on the cardPile that is indicated. addCard would look something like this:

void cardPile::addCard(Card scrd)  //add a card the this cardPile
{
   theDeck.push_back(scrd);
}


and listCard would look something like this:

void CardPile::listCards()  //list cards in this cardPile
{
	cout << "This is a test" << endl;
	for(int i=0; i < theDeck.size(); i++)
	{
		cout << theDeck[i].rank << " " << theDeck[i].suit;
	}
}

As you have it, destP1 and lst are separate vectors from theDeck, which makes no sense at all to me.

bool Card::operator== (const Card &rhs)
{
	return ((Card::suit == rhs.suit) && (Card::rank == rhs.rank));
}

Make up your mind . . . is Card::suit static or not? And the same with rank. :)

I do wonder if this is still relevant to answer... An overview of the code sugests you have no idea how obejct orientation works in c++ ? For example CardPile() is pointless (it does nothing but initializes a vector and destroys it)...

If we assume that you use TheDeck as simply a holder for a vector you should be able to get something out of listCards if you actually do something more than initalizing and destroying cards in initDeck (ie add the card to the deck after you have created it), I have no idea if this is what you want but it should atleast show some results that way...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.