2016-01-28 45 views
-3

我正在用C++編寫一個基本的卡片系統。當然,它是面向對象的。我所做的只是一個卡堆棧的類,另一個是卡本身和主類。但是當我試圖做這個項目時,我注意到我製作的所有卡片都是一樣的!在我看來,問題在於創建對象本身。檢查出來,球員:(C++)即使我給出不同的構造函數參數,也無法創建對象不同的對象

Main.cpp的

#include "Header.h" 

using namespace std; 


char cardTypes[4] = { CARD_TYPE_HEARTS,CARD_TYPE_SPADES,CARD_TYPE_CLUBS,CARD_TYPE_DIAMONDS }; 

int main(void) { 

    setlocale(LC_ALL, "Portuguese"); 
    srand((unsigned)time(NULL)); 

    CardStack stack; 
    for (int i = 0; i < 4; i++) { 
     char type = cardTypes[i]; 
     for (int j = 0; j <= 13; j++) { 
      stack.add(&Card(j, type)); 
     } 
    } 
    stack.shuffle(); 

    cout << stack.getCard(0)->translateMe() << endl; 
    cout << stack.getCard(1)->translateMe() << endl; 
    cout << stack.getCard(2)->translateMe() << endl; 

    return 0; 
} 

Header.h

#ifndef HEADER_H 
#define HEADER_H 

#include <iostream> 
#include <vector> 
#include <time.h> 
#include <random> 
#include <string> 

using namespace std; 

#define CARD_POS_STACK 0 

#define CARD_TYPE_HEARTS 'H' 
#define CARD_TYPE_SPADES 'S' 
#define CARD_TYPE_CLUBS 'C' 
#define CARD_TYPE_DIAMONDS 'D' 
#define CARD_TYPE_NONE ' ' 

#define CARD_JOKER 0 
#define CARD_ACE 1 
#define CARD_2 2 
#define CARD_3 3 
#define CARD_4 4 
#define CARD_5 5 
#define CARD_6 6 
#define CARD_7 7 
#define CARD_8 8 
#define CARD_9 9 
#define CARD_10 10 
#define CARD_JACK 11 
#define CARD_QUEEN 12 
#define CARD_KING 13 

class Card { 

private: 

    char type; /* type can be H (hearts), S (spades), C (clubs) or D (diamonds) */ 
    short num; /* coringa = 0, J = 11, Q = 12 and K = 13*/ 
    int pos; /* 0 = stack */ 

public: 

    /* > Creates a card */ 
    Card(short num, char type); 

    /* > Recieves the card's type */ 
    char getType(); 

    /* > Recieves the card's number */ 
    short getNum(); 

    /* > Translates the number */ 
    string translateMe(); 

    /* > Recieves the card's position on the table */ 
    int getPos(); 

    /* > Checks if a card is equal to another */ 
    bool isEqual(Card* another); 
}; 

class CardStack { 

private: 

    vector<Card*> cards; 
    int numOfCards; 

public: 

    int getSize(); 

    /* > Checks if there is any item in the stack */ 
    bool isEmpty(); 

    /* > Add a card to the top of the stack */ 
    void add(Card* theCard); 

    /* > Remove a card from the stack */ 
    void remove(Card* theCard); 

    /* > Shuffles randomly the card */ 
    void shuffle(); 

    /* > Get a certain card */ 
    Card* getCard(int i); 

    /* > Gets the card at the top of the stack */ 
    Card* getTopCard(); 

    /* > Generates an empty stack */ 
    CardStack(); 

}; 

#endif 

Card.cpp

#include "Header.h" 

Card::Card(short cardNum, char cardType){ 
    num = cardNum; 

    if (cardNum = CARD_JOKER) 
     type = CARD_TYPE_NONE; 
    else 
     type = cardType; 

    pos = CARD_POS_STACK; 
} 

string Card::translateMe() { 
    string message = ""; 

    switch (num) { 
    case 0: 
     message.append("Coringa"); 
     break; 
    case 11: 
     message.append("Valete de"); 
     break; 
    case 12: 
     message.append("Rainha de "); 
     break; 
    case 13: 
     message.append("Rei de "); 
     break; 
    default: 
     message.append(to_string(num)+" de "); 
    } 

    switch (type) { 
    case CARD_TYPE_CLUBS: 
     message.append("Paus"); 
     break; 
    case CARD_TYPE_DIAMONDS: 
     message.append("Ouros"); 
     break; 
    case CARD_TYPE_HEARTS: 
     message.append("Copas"); 
     break; 
    case CARD_TYPE_SPADES: 
     message.append("Espadas"); 
     break; 
    } 

    return message; 
} 

char Card::getType() { return type; } 
short Card::getNum() { return num; } 
int Card::getPos() { return pos; } 
bool Card::isEqual(Card* another) { return this == another; } 

CardStack.cpp

#include "Header.h" 
#include <algorithm> 

bool CardStack::isEmpty() { 
    return numOfCards == 0 ? true : false; 
} 

void CardStack::add(Card* theCard) { 
    cards.push_back(theCard); 
} 

void CardStack::remove(Card* theCard) { 
    for (int i = 0; i < cards.size(); i++) 
     if (theCard->isEqual(cards[i])) 
      cards.erase(cards.begin() + i); 
} 

CardStack::CardStack() { 
    numOfCards = 0; 
} 

void CardStack::shuffle() { 
    random_shuffle(cards.begin(), cards.end()); 
} 

Card* CardStack::getCard(int i) { 
    return cards.at(i); 
} 

Card* CardStack::getTopCard() { 
    return cards.front(); 
} 

int CardStack::getSize() { 
    numOfCards = cards.size(); 
    return numOfCards; 
} 

和輸出如下:

"Rei de Ouros" 
"Rei de Ouros" 
"Rei de Ouros" 
+0

你爲什麼使用'卡'指針?爲什麼不只是「卡」對象? – PaulMcKenzie

+0

要添加,如果您將整個實現更改爲使用'Card'而不是'Card *',我打賭您的程序將工作(或更接近工作)。另外,不需要'numOfCards'成員變量。你有一個'vector ',一個向量知道它自己的大小。有不必要的變量表示向量中的項目數量只是在腳中拍攝自己的另一種方式(如果向量的大小發生變化,則不更新此變量)。如果你想知道條目的數量,只需使用'vector :: size()'。 – PaulMcKenzie

回答

1

在這一部分:

stack.add(&Card(j, type)); 

你正在採取臨時對象的地址。

更改該行:

stack.add(new Card(j, type)); 

和它的作品!

但它會泄漏內存。相反,你可能應該使用CardStack持卡而不是卡指針。

+0

它也會泄漏內存! – PaulMcKenzie

+0

_「但它會泄漏內存」_ - 所以建議使用'std :: unique_ptr'。 –

相關問題