So I have two classes, a game class and a blackjack class, they both look as follows:

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?
What fixed it for me was to change
Code:
class Blackjack : public Card {
to
Code:
class Blackjack : public Game {
Then gcc started complaining about not having a main() function, so it still wouldn't compile, but that fixed the access errors. I also had to change
Code:
class Game {

        private:
to
Code:
class Game {

        protected:
Wow, as long as I have been trying to figure this out how could I have missed something that simple. Thank you for finding that.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement