Reputation: 83
As the title suggests I am trying to send a vector from one class to another.. Here is some sample code...
class Deck
{
public:
//constructor
Deck(){}
~Deck(){} //deconstructor
//global for setting deck size
int SetDeckMultiplier;
//Card SetUp
//access vector
std::vector<int> GetCopyOfVector(){return cards;}
//set deck size based on multiplier
void SetDeckSize();
//shuffle card vector
void shuffle();
//End Card Setup
protected:
private:
std::vector <int> cards;
};
This essentially sets up a vector, fills it with ints, than shuffles the ints.
I have a second class...
class Dealer: public Deck
{
public:
void Hit();
void GetStoredDeck()
{
std::cout << cards.size() << std::endl;
}
void DealCard();
void DealerScore();
void IniDeal();
void CardPositions();
private:
};
I am attempting to pull the vector into this class, so that dealer can use the ints. I can make the vector protected, but the .size() is 0. Is there a way to pass it into this class after it is filled and shuffled in the first class?
Edit:
I do not have much code for the functions in classes. I was just working off a skeleton setup.
Deck.cpp
void Deck::SetDeckSize()
{
for (int i = 0; i < (52 * SetDeckMultiplier); i++)
{
cards.push_back(i);
}
//set card values
for (int i = 0; i > (52 * SetDeckMultiplier); i++)
{
cards[i] = i;
}
}
void Deck::shuffle()
{
for (int i = 0; i < (52 * SetDeckMultiplier); i++)
{
int r = i + (rand() % ((52 * SetDeckMultiplier) - i)); // Random remaining position.
int temp = cards[i]; cards[i] = cards[r]; cards[r] = temp; //swap cards
while (cards[i] >= 52) cards[i] = cards[i] - 52; //loop to ensure correct number for display
//std::cout << cards[i] << std::endl;//display random cards.. for now
}
}
This is the code I have to fill the vector and shuffle the elements.
I would like to take the shuffled vector and use it in the dealer class. This way the dealer can get the next element in the vector and do what need to be done with it.
What I was saying about the size() is that if I make the vector protected instead of private, I can use the vector via inheritance, but it is the empty vector not the filled and shuffled one.
Upvotes: 2
Views: 4321
Reputation: 350
Starting with your code (and assuming you have made cards
protected in Deck
), you could use your Dealer class like this:
Dealer dealer;
dealer.SetDeckSize();
dealer.shuffle();
dealer.GetStoredDeck();
However, your class design is not looking good, as pointed out by @Troy.
A dealer is not a deck, so Dealer
should not be a subclass of Deck
. Instead, Dealer
should contain a field of the Deck class, like this:
class Dealer
{
public:
void Hit();
void DealCard();
void DealerScore();
void IniDeal();
void CardPositions();
private:
Deck deck;
};
Upvotes: 1
Reputation: 45470
What you need to do is add create cards into cards
in Deck constructor. This is just a simple sample which shows shared data among base and derived class:
class Deck
{
public:
Deck() : cards{1,2} {} // C++11 supports initializer list
virtual ~Deck() {} // better declare base destructor as virtual
protected: // derived class can access protected base members
std::vector <int> cards;
};
class Dealer: public Deck
{
public:
Dealer() // Deck default constructor is called
{
}
void GetStoredDeck()
{
std::cout << cards.size() << std::endl;
}
};
int main(int argc, char* argv[])
{
Dealer dealer;
dealer.GetStoredDeck(); // you should see 2 cards now?
return 0;
}
Upvotes: 1
Reputation: 36
In order to access to int vector cards
in your derived class Dealer
, you must declare it protected
...
From your methods name, I guess Deck::SetDeckSize()
is the guy that puts ints into cards
.
So, I'd guess that you haven't called this method before calling Dealer::GetStoredDeck()
.
Upvotes: 0