class Deck
{
private: vector<Card> cardvector; vector<Card *> pcardvector;
public:
Deck (vector<Card> cardvector2)
{
cardvector = cardvector2;
for (int x = 0; cardvector.size(); ++x)
{
pcardvector[x] = &cardvector[x];
}
}
void printdeck ()
{
for (int x; x < 52; ++x)
{
switch(cardvector[x].getvalue())
{
case 1: cout << "Ace"; break;
case 11: cout << "Jack"; break;
case 12: cout << "Queen"; break;
case 13: cout << "King"; break;
default: cout << cardvector[x].getvalue(); break;
}
cout << " of ";
cout << cardvector[x].getsuit() << endl;
}
}
as you can see the printdeck function isn't using pointers yet, i need it to print using the pointers created in the constructor, just in case, here is the class of the card:
class Card
{
private: string suit; int value;
public:
Card()//default constructor (no arguments) allows you to make an
{ //array
value = 0;
}
Card(int v, string s)// allows the private attributes to be set
{
value = v;
suit = s;
}
int getvalue()// returns the cards value
{
return value;
}
string getsuit()// returns the cards suit
{
return suit;
}
};
and this is the my main (includes the function that creates the deck):
//where is the stop condition x < cardvector.size()
for (int x = 0; cardvector.size(); ++x)
{
pcardvector[x] = &cardvector[x]; //and this is a problem see below.
}
pcardvector contains no elements, its size is 0, trying to index into the vector is going to fail. You will need to push_back(&cardvector[x])
1 2
//x is not initialized here
for (int x; x < 52; ++x)
Once you make these changes call the pointer vector like you think it should be called.
it has warnings where the for loop arguement is, saying : trying to compare signed and unsigned variables, and the program doesnt actually output anything
#include <iostream>
#include <string>
usingnamespace std;
class Card
{
private: string suit; int value;
public:
Card()//default constructor (no arguments) allows you to make an
{ //array
value = 0;
}
Card(int v, string s)// allows the private attributes to be set
{
value = v;
suit = s;
}
int getvalue()// returns the cards value
{
return value;
}
string getsuit()// returns the cards suit
{
return suit;
}
};