Reputation: 3431
I'm fairly new to C++ and I'm running into problems trying to use one class object in another class function. For example if I have:
#ifndef CARD_HPP
#define CARD_HPP
#include <string>
enum Suits
{
SPADES,
CLUBS,
HEARTS,
DIAMONDS
};
enum Values
{
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE
};
class Card
{
public:
Card(Values value, Suits suit);
Values getValue() const;
Suits getSuit() const;
void showCard();
std::string toString(Values v);
std::string toString(Suits s);
private:
Suits suit;
Values value;
};
and:
#ifndef DECK_OF_CARDS_HPP
#define DECK_OF_CARDS_HPP
#include "card.hpp"
#include <vector>
#include <string>
Values enumValueOfIndex(int i);
Suits enumSuitOfIndex(int i);
class DeckOfCards
{
public:
DeckOfCards();
void shuffleDeck();
void printDeck();
Card drawCard();
private:
const std::string suit_strings[4];
const std::string value_strings[13];
unsigned seed;
Values values;
Suits suits;
std::string suit;
std::string value;
std::vector<Card> deck;
};
#endif
I'm having an error when in my Deck::printDeck()
when trying to do:
Card card = deck.at(i); //I have already added 52 card objects to deck
The error I am getting is:
error: non-object type 'Card (Values, Suits)' is not assignable
card = deck.at(i);
I am essentially using old Java code to make this and that is essentially what I did. I don't know if I need to define/declare a Card
object in my Deck
header/source or even how to do that. I feel like this is a simple problem but I can't find any solution to it. Thank you for any help!
Upvotes: 0
Views: 71
Reputation: 14573
Defining a copy-assignment constructor for your class will make the =
(assignment operator) to run in your program.
Card( const Card & input );
If you want to assign a special meaning for the =
operator, then you should override it. Below you can find some resources that will help you:
Upvotes: 0
Reputation: 1
Looks like you need to define a copy constructor and assignment operator, try adding:
Card(const Card& rhs);
Card& operator=(const Card& rhs);
Upvotes: 1