I'm having an issue with inheritance in C++.
I have a "deckOfCards" object that I'm trying to inherit vector from. I'm doing this because I want the "deckOfCards" object itself to be a collection.
Only issue is the code won't let me add objects using the "this" keyword unless I inherit from "vector card* " not "vector Card".
Here is the class code for "deckOfCards":
#include "stdafx.h"
#include <vector>
#include <typeinfo>
#include <iostream>
//#include "Card.cpp"
using namespace System;
using namespace System::Collections::Generic;
using namespace std;
/// <summary>
/// Deck of cards class
/// </summary>
///
///
///
public enum deckTypeEnum { Full = 1 };
public enum cardSuit { Hearts = 1, Diamonds = 2, Clubs = 3, Spades = 4 };
public enum cardRank { Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, Jack = 11, Queen = 12, King = 13 };
public class Card
{
public: Card(cardSuit suit, cardRank rank)
{
CardSuit = suit;
CardRank = rank;
}
public: cardSuit CardSuit;
public: cardRank CardRank;
};
public class DeckOfCards: public vector<Card*>
{
public:DeckOfCards::DeckOfCards(deckTypeEnum deckType)
{
InitCardDeck(deckType);
}
public: void InitCardDeck(deckTypeEnum deckType)
{
this->push_back(new Card(Hearts, Four));
switch (deckType)
{
case 1:
CardCount = 52;
GetFullCardDeck();
break;
default:
CardCount = 52;
GetFullCardDeck();
break;
}
}
public: void GetFullCardDeck()
{
for (int i=0; i<Spades; i++)
{
for (int g=0; g<King; g++)
{
this->push_back(new Card((cardSuit)i, (cardRank)g));
}
}
}
public: void Shuffle()
{
Random^ rand = gcnew Random();
for (int i = CardCount - 1; i > 0; i--)
{
int n = rand->Next(i + 1);
Card *temp = this->at(i);
this->at(i) = this->at(n);
this->at(n) = temp;
}
}
public: int CardCount;
// public: vector<Card> deckocards1;
//deckocards1[1];
};
This presents a problem later on where I'm moving 5 objects from the "deckOfCards" object to another vector (player.CardHand). The vector (player.CardHand) is of type "vector Card". So it doesn't match the "deckOfCards" type of "vector Card* ". I get an error as a result.
move(deckOfCards.begin(), it, back_inserter(player.CardHand)); // ##
An easy resolution would be to make Player.Hand of type "vector Card" and not "vector Card* ".
The reason Player.Hand has to be of type "vector Card" and not "vector Card* " is because I need to do a switch statement on each items in the collection and if it is "vector Card* " I get the error "expression must have class type".
public: int GetScore(vector<Card> cardHand)
{
int score = 0;
for (int i=0;i<cardHand.size();i++)
{
switch (cardHand[i].CardRank)
{
case King:
score += 10;
break;
case Queen:
score += 10;
break;
case Jack:
score += 10;
break;
//Considering an Ace is equal to one, it doesn't mention it in the instructions.
default:
score += (int)cardHand[i].CardRank;
break;
}
}
return score;
}
Edit
To simplify the question the issue I'm having is here:
I'm getting an error saying expression must have class type on the "CardHand" object.
public: int GetScore(vector<Card*> cardHand)
{
int score = 0;
for (int i=0;i<cardHand.size();i++)
{
switch (cardHand.CardRank)
{
case King:
score += 10;
break;
case Queen:
score += 10;
break;
case Jack:
score += 10;
break;
//Considering an Ace is equal to one, it doesn't mention it in the instructions.
default:
score += (int)cardHand[i].CardRank;
break;
}
}
return score;
}