OOP问题 - 派生类

时间:2014-10-02 23:22:43

标签: c++ oop derived-class undefined-reference playing-cards

我试图找出对Class :: Function()的未定义引用。该项目是我已经看过一些回复的项目,我查看了其他人的回复,看起来代码相似,但我似乎无法找到我的具体问题的答案。我正在尝试使用2 .h文件,它们的实现文件和驱动程序来简单地处理卡片中的卡片。我已经碰壁,因为我并不是100%确定如何使用派生类。编译时我当前的错误如下:

  • 未定义参考' Deck :: shuffleCards()'
  • 未定义参考' Deck :: dealCard()'
  • 未定义参考' Card :: getCard()const'

非常感谢提供的任何帮助。这是代码

CARD.H

#ifndef CARD_H
#define CARD_H
#include <string>

using namespace std;

class Card
{

    private:
    string face; //Used to store value of face(A-K)
    string suit; //User to store value of suit(A/R/S/B)

    public:

    string getCard() const; //Returns the card.

    //Constructor
    Card(string cardFace, string cardSuit)
    {face = cardFace; suit = cardSuit;}

    //Default Constructor
    Card()
    {}
};
#endif // CARD_H

Card.cpp

#include <iostream>
#include "CARD.H"

string Card::getCard() const
{
    //Returns the card in a format of X of Y(Ace of Shields)
    return (face + " of " + suit);
}

加入deck.h

#ifndef DECK_H
#define DECK_H
#include "CARD.H"
#include <string>


using namespace std;

const int SIZE = 52;  //Standard Size of Deck. Used for calculations

class Deck : private Card
{

    private:
    Card *deckOfCards; //Creates pointer for deck.
    int currentCard; //Keeps track of the current card array location

    public:
     //Constructor for Deck. Creates a deck of cards.
    Deck() : Card()
    {
        string faces[] = {"Ace", "Two", "Three", "Four", "Five", "Six",
                    "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        string suits[] = {"Acorns", "Shields", "Bells", "Roses"};

        deckOfCards = new Card[SIZE];
        currentCard = 0;
        for(int i = 0; i < SIZE; i++)
        {
            deckOfCards[i] = Card(faces[i % 13], suits[ i / 13]);
        }

    }

        void shuffleCards();  //Shuffles the cards.
        Card dealCard(); //Returns a Card data type when this is called.
        void printDeck() const; //Prints the deck.
};
#endif // DECK_H

Deck.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DECK.H"
#include "CARD.H"

/**********************************************************************
* Uses 2 arrays to shuffle the cards. The function uses
* 2 arrays and random number generation with a modulus of the size of
* the deck to find the next location of the card.
**********************************************************************/
void Deck::shuffleCards()
{
currentCard = 0;

for(int first = 0; first < SIZE; first ++)
    {
        int second = (rand() + time(0)) % SIZE;
        Card temp = deckOfCards[first];
        deckOfCards[first] = deckOfCards[second];
        deckOfCards[second] = temp;
    }

}

Card Deck::dealCard()
{
//Checks to make sure the current card location is not greater than size.
//If so, this would indicate the deck needs to be shuffled because we're
// out of cards
    if (currentCard > SIZE)
        shuffleCards();
    if (currentCard < SIZE)
        return(deckOfCards[currentCard++]);

    return deckOfCards[0];
}

 //Function used to format the printing of the deck.
 void Deck::printDeck() const
 {
     for(int i = 0; i < SIZE; i++)
     {
         cout << deckOfCards[i].getCard();
         if((i + 1) % 5 == 0) //Prints 5 cards per line.
         {
            cout << endl; //If mod expression is valid, starts new line.
         }
     }
 }

主要驱动程序

#include <iostream>
#include <string>
#include "DECK.H"
#include "CARD.H"

using namespace std;

int main()
{
    Deck myDeck;
    Card myCard;


    myDeck.shuffleCards();

    for(int i = 0; i< 52; i++)
    {    
        myCard = myDeck.dealCard();
        cout << myCard.getCard() << endl;
    }

    return 0;
}

0 个答案:

没有答案