C ++ - 类型重定义错误&使用未定义的类错误

时间:2017-09-06 09:52:13

标签: c++ class

这是我第一次使用C ++而且我真的遇到了这两个错误。

我有两个类及其头文件,Deck.cpp,Card.cpp,Deck.h,Card.h

第一个问题:我在使用Deck课程中的Card课程时出现问题(这是我从错误中理解的)。

第二个问题:我遇到了Card类的重新定义错误。我没有发现自己定义了两次Card类,所以我不确定为什么会出现这个错误。

Deck.cpp:

#include "stdafx.h"
#include "Deck.h"
#include <algorithm>    
#include <vector>       
#include <ctime>        
#include <cstdlib> 
using namespace std;

Deck::Deck()
{ 
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii - 1].setCard('S', (ii + '0'));
    }
    deck[10].setCard('S', 'J');
    deck[11].setCard('S', 'Q');
    deck[12].setCard('S', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 12].setCard('D', (ii + '0'));
    }
    deck[23].setCard('D', 'J');
    deck[24].setCard('D', 'Q');
    deck[25].setCard('D', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 25].setCard('C', (ii + '0'));
    }
    deck[36].setCard('C', 'J');
    deck[37].setCard('C', 'Q');
    deck[38].setCard('C', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 38].setCard('H', (ii + '0'));
    }
    deck[49].setCard('H', 'J');
    deck[50].setCard('H', 'Q');
    deck[51].setCard('H', 'K');
    startingIndex = 0;
    topCard = deck[startingIndex];
}

Card Deck::deal()
{
    Card tempTopCard = topCard;
    topCard = deck[++startingIndex];
    return tempTopCard;
}

int Deck::cardsLeft()
{
    return 52 - startingIndex;
}

void Deck::refreshDeck()
{
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii - 1].setCard('S', (ii + '0'));
    }
    deck[10].setCard('S', 'J');
    deck[11].setCard('S', 'Q');
    deck[12].setCard('S', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 12].setCard('D', (ii + '0'));
    }
    deck[23].setCard('D', 'J');
    deck[24].setCard('D', 'Q');
    deck[25].setCard('D', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 25].setCard('C', (ii + '0'));
    }
    deck[36].setCard('C', 'J');
    deck[37].setCard('C', 'Q');
    deck[38].setCard('C', 'K');
    for (int ii = 1; ii <= 10; ++ii)
    {
        deck[ii + 38].setCard('H', (ii + '0'));
    }
    deck[49].setCard('H', 'J');
    deck[50].setCard('H', 'Q');
    deck[51].setCard('H', 'K');
    startingIndex = 0;
    topCard = deck[startingIndex];
}

void Deck::showDeck()
{
    for (int ii = startingIndex; ii <= 51; ++ii)
    {
        deck[ii].displayCard();
    }
}

void Deck::shuffle()
{
    srand(time(0));
    random_shuffle(&deck[0], &deck[51], rand());
}

加入deck.h:

#define Deck_H
#include "Card.h"

class Deck
{
public:
    Deck();

    Card deal();
    int cardsLeft();
    void refreshDeck();
    void showDeck();
    void shuffle();

private:
    Card deck[51];
    Card topCard;
    int startingIndex;
};

Card.cpp:

#include "stdafx.h"
#include "Card.h"
#include <iostream>
using namespace std;

Card::Card()
{
    suit = rank = 0;
}

int Card::getValue() 
{
    if (isdigit((int)rank))
        return rank - '0';
    else if (rank = 'J')
        return 11;
    else if (rank = 'Q')
        return 12;
    else
        return 13;
}

void Card::setCard(char newSuit, char newRank)
{
    suit = newSuit;
    rank = newRank;
}

void Card::displayCard()
{
    cout << rank << suit;
}

Card.h:

#define Card_H

class Card
{
public:
    Card(); 
    Card(char suit, char rank) : suit(suit), rank(rank) { }

    int getValue();
    void setCard(char suit, char rank);
    void displayCard();

private:
    char suit, rank;
};

我发现这4个错误,我找不到任何解决方法:

1>d:\cecs 282\program 1\Card.h(10): error C2011: 'Card': 'class' type redefinition 
1>d:\cecs 282\program 1\Card.h(10): note: see declaration of 'Card' 
1>d:\cecs 282\program 1\Deck.h(22): error C2079: 'Deck::deck' uses undefined class 'Card' 
1>d:\cecs 282\program 1\Deck.h(23): error C2079: 'Deck::topCard' uses undefined class 'Card'

有人请帮忙,因为我一直在寻找好几个小时而且感到非常沮丧。

0 个答案:

没有答案