So I have two classes, a game class and a blackjack class, they both look as follows:
game.h
Code:
game.cpp
Code:
blackjack.h
Code:
blackjack.cpp
Code:
When I try to compile I get the following error:
cannot call member function ‘void Game::shuffle()’ without object
I have tried making shuffle protected, but that didn't work either. I have tried searching for the answer already, but can not find one. Is there anyway I can call the shuffle method from within my blackjack class?
game.h
Code:
#ifndef GAME_H
#define GAME_H
#include "card.h"
class Game {
private:
int m_card_array[52];
void shuffle();
public:
Game();
virtual void run() = 0;
virtual Card draw() = 0;
};
#endif
game.cpp
Code:
#include <SDL/SDL.h>
#include <iostream>
#include "game.h"
#include <stdlib.h>
Game::Game() {
/*
I am not using traditional number of 1-52 so I cannot populate in that
manner.
1-13 = clubs
21-33 = hearts
41-53 = spades
61-73 = diamonds
*/
int i = 1;
for(int j = 0; j < 13; j++) {
m_card_array[j] = i;
i++;
}
i = 21;
for(int j = 13;j < 26;j++) {
m_card_array[j] = i;
i++;
}
i = 41;
for(int j = 26; j < 39; j++) {
m_card_array[j] = i;
i++;
}
i = 61;
for(int j = 39; j < 52; j++) {
m_card_array[j] = i;
i++;
}
}
void Game::shuffle() {
srand(time(NULL));
int temp = 0;
int j = 0;
for(int i = 51; i >= 0; i++) {
j = (rand() % 51);
temp = m_card_array[i];
m_card_array[i] = m_card_array[j];
m_card_array[j] = temp;
}
}
blackjack.h
Code:
#ifndef BLACKJACK_H
#define BLACKJACK_H
#include "game.h"
#include "card.h"
class Blackjack : public Card {
private:
public:
void run();
Card draw();
};
#endif
blackjack.cpp
Code:
#include "blackjack.h"
#include "card.h"
#include <iostream>
void Blackjack::run() {
Game::shuffle();
std::cout << "You are running blackjack!" << std::endl;
}
Card Blackjack::draw() {
}
When I try to compile I get the following error:
cannot call member function ‘void Game::shuffle()’ without object
I have tried making shuffle protected, but that didn't work either. I have tried searching for the answer already, but can not find one. Is there anyway I can call the shuffle method from within my blackjack class?