(C ++)即使我提供不同的构造函数参数,也无法创建不同对象的对象

时间:2016-01-28 21:08:31

标签: c++ object

我正在用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"

1 个答案:

答案 0 :(得分:1)

在这部分:

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

您正在获取临时对象的地址。

将该行更改为:

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

它有效!

但它会泄漏内存。相反,您应该使用CardStack保留卡而不是卡指针。