Reputation: 88
Thanks for reading my question.
I'm trying to begin writing a program that uses a deck of cards, because I want to learn about using classes. I decided to create a class that stores a suit and a value, like so:
#ifndef CARD_H
#define CARD_H
class Card {
public:
//here are the individual card values
int _suit;
int _value;
Card(int suit, int value);
};
#endif
#include "card.h"
//define suits
const int spades = 0;
const int clubs = 1;
const int hearts = 2;
const int diamonds = 3;
//define face cards
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
Card::Card(int suit, int value){
_suit = suit;
_value = value;
}
I then decided to create a deck class, and in that class initialize my cards by assigning a value from 0-3 (suit) and then a value from 2-14 (ranking of card). I also add the cards into a vector that I defined to hold them, like so:
#ifndef DECK_H
#define DECK_H
#include <vector>
class Deck {
public:
std::vector<Card> _deck(51);
Deck();
};
#endif
#include <iostream>
#include <vector>
#include "card.h"
#include "deck.h"
const int all_suits = 4 - 1;
const int all_values = 14;
Deck::Deck(){
int i = 0;
for (int j = 0; j <= all_suits; ++j) {
//begin with 2 because card can't have value 0
for (int k = 2; k <= all_values; ++k) {
_deck[i] = Card(j, k);
++i;
}
}
}
This somehow gives me issues when I try to test the program like so:
#include <iostream>
#include <vector>
#include "card.h"
#include "deck.h"
int main() {
Deck new_deck = Deck();
std::cout << new_deck._deck[4]._value;
return 0;
}
The following errors show up when I try to run the code:
In file included from main.cpp:6:
./deck.h:8:27: error: expected parameter declarator
std::vector<Card> _deck(51);
^
./deck.h:8:27: error: expected ')'
./deck.h:8:26: note: to match this '('
std::vector<Card> _deck(51);
^
main.cpp:22:24: error: reference to non-static member function must be called; did you mean to call it with no arguments?
std::cout << new_deck._deck[4]._value;
~~~~~~~~~^~~~~
()
3 errors generated.
In file included from deck.cpp:8:
./deck.h:8:27: error: expected parameter declarator
std::vector<Card> _deck(51);
^
./deck.h:8:27: error: expected ')'
./deck.h:8:26: note: to match this '('
std::vector<Card> _deck(51);
^
deck.cpp:18:4: error: reference to non-static member function must be called; did you mean to call it with no arguments?
_deck[i] = Card(j, k);
^~~~~
()
3 errors generated.
Honestly, I'm not quite sure what's going on. I pretty closely followed an example of classes I saw online. If anyone could help me find the problem, that would be much appreciated. Sorry, I'm a little new to this, and may have made some silly mistakes. I really appreciate your time and patience in reading this and helping me out.
Upvotes: 2
Views: 1990
Reputation: 7111
Your problem lies with the declaration of _deck
:
class Deck {
public:
std::vector<Card> _deck(51);
Here you are trying to declare _deck
and call a constructor, but you can not do so from a class member initialiser. It's possible to do this for data members with no constructors, for example:
class Deck {
public:
int no_of_cards = 51;
std::vector<Card> _deck;
You will need to use your constructor to initialise _deck
(much like you're already doing:
Deck::Deck() {
_deck.reserve(51);
int i = 0;
for (int j = 0; j <= all_suits; ++j) {
//begin with 2 because card can't have value 0
for (int k = 2; k <= all_values; ++k) {
_deck.push_back(Card(j, k));
++i;
}
}
}
And leave your declaration of _deck
as:
class Deck {
public:
std::vector<Card> _deck;
Upvotes: 3