I'm having trouble understanding why I can't access the member functions of a vector that I've referenced into a function. I'm trying to overide the << operator for a class I've made that is meant to hold some card objects.
Deck.h
#ifndef _deck_h
#define _deck_h
#include "card.h"
#include <vector>
/// This class provides several functions for creating and handling a deck of cards.
using namespace std;
class Deck
{
public:
/// \brief
///
/// No Argument constructor – Creates a dynamic array of Card objects and initialises them. Initialises cardsDealt to 0.
///
Deck();
/// \brief
///
/// Destructor – deletes the dynamic array
///
~Deck();
/// \brief
///
/// Gets the next card from the deck
///
/// \return Card - AD, 9S, ... etc
///
Card dealNextCard();
/// \brief
///
/// Shuffles the cards in the deck
///
void shuffle();
/// \brief
///
/// Sends a string representation of the cards in the deck to the output stream.
///
friend ostream& operator<<(ostream& out, Deck& deck);
private:
vector<Card> deck;
int cardsDealt;
};
#endif // _deck_h
Deck.cpp
#include "deck.h"
#include "card.h"
#include <iostream>
#include <algorithm>
//Creates a dynamic array of Card objects and initialises them. Initialises cardsDealt to 0.
Deck::Deck()
{
cardsDealt = 0;
for(int suits = CLUBS; suits <= SPADES; suits++)
{
for(int ranks = TWO; ranks <= ACE; ranks++)
{
deck.push_back(Card(static_cast<Rank>(ranks), static_cast<Suit>(suits)));
}
}
}
//Destructor
Deck::~Deck()
{
//~No objects to be deleted
}
//Gets the next card from the deck
Card Deck::dealNextCard()
{
Card cardDealt = deck.back();
deck.pop_back();
return cardDealt;
}
//Shuffles the cards in the deck
void Deck::shuffle()
{
random_shuffle(deck.begin(), deck.end());
}
//Sends a string representation of the cards in the deck to the output stream.
ostream& operator<<(ostream& out, Deck& deck)
{
deck.begin();
}
main.cpp
/*
* File: shuffle.cpp
* Creates a deck of cards, shuffles them and displays them.
*/
#include <iostream>
#include <iomanip>
#include "deck.h"
using namespace std;
int main() {
Deck deck;
cout << deck << endl << endl;
deck.shuffle();
cout << deck << endl << endl;
return 0;
}
The problem occurs at the bottom of my Deck.cpp file. When the program is run it gives me "error 'class Deck' has no member named 'begin'" Which is true that it doesn't, but I thought I was passing in a reference to the vector<Card> deck variable that I make in the Decks constructor which does have access to all of the vectors functions as you can imagine.
So is the only way I can get the functionality of a vector in my reference to copy the functions over into my Deck class like so:
Card Deck::getLastCard()
{
return deck.back();
}
for every method or is there a simpler way. All I want to do is iterate over the cards in the Deck and present them on screen but this minor reference has stopped me dead in my tracks.