我需要帮助使用带有类的数组

时间:2017-04-27 15:52:40

标签: c++

当我尝试编译时,我收到错误“未定义引用'Card :: Card()'”。在我初始化每个卡片后,该程序应该打印在卡片中的所有卡片。它必须使用OOP,所以我不需要改变它。帮助任何人!

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

class Card
{

private:
string suit;
string value;

public:
    Card();

    Card(string a, string b)
    {
        suit = a;
        value = b;
    }

    string getSuit()
    {
        return suit;
    }

    string getValue()
    {
        return value;
    }

    void print()
    {
        cout << getSuit() << " of " << getValue() << endl;
    }

};


int main()
{
    Card cardlist[52];

    cardlist[0] = Card("2","Spades");
    cardlist[1] = Card("3","Spades");
    cardlist[2] = Card("4","Spades");
    cardlist[3] = Card("5","Spades");
    cardlist[4] = Card("6","Spades");
    cardlist[5] = Card("7","Spades");
    cardlist[6] = Card("8","Spades");
    cardlist[7] = Card("9","Spades");
    cardlist[8] = Card("10","Spades");
    cardlist[9] = Card("Jack","Spades");
    cardlist[10] = Card("Queen","Spades");
    cardlist[11] = Card("King","Spades");
    cardlist[12] = Card("Ace","Spades");
    cardlist[13] = Card("2","Hearts");
    cardlist[14] = Card("3","Hearts");
    cardlist[15] = Card("4","Hearts");
    cardlist[16] = Card("5","Hearts");
    cardlist[17] = Card("6","Hearts");
    cardlist[18] = Card("7","Hearts");
    cardlist[19] = Card("8","Hearts");
    cardlist[20] = Card("9","Hearts");
    cardlist[21] = Card("10","Hearts");
    cardlist[22] = Card("Jack","Hearts");
    cardlist[23] = Card("Queen","Hearts");
    cardlist[24] = Card("King","Hearts");
    cardlist[25] = Card("Ace","Hearts");
    cardlist[26] = Card("2","Diamonds");
    cardlist[27] = Card("3","Diamonds");
    cardlist[28] = Card("4","Diamonds");
    cardlist[29] = Card("5","Diamonds");
    cardlist[30] = Card("6","Diamonds");
    cardlist[31] = Card("7","Diamonds");
    cardlist[32] = Card("8","Diamonds");
    cardlist[33] = Card("9","Diamonds");
    cardlist[34] = Card("10","Diamonds");
    cardlist[35] = Card("Jack","Diamonds");
    cardlist[36] = Card("Queen","Diamonds");
    cardlist[37] = Card("King","Diamonds");
    cardlist[38] = Card("Ace","Diamonds");
    cardlist[39] = Card("2","Clubs");
    cardlist[40] = Card("3","Clubs");
    cardlist[41] = Card("4","Clubs");
    cardlist[42] = Card("5","Clubs");
    cardlist[43] = Card("6","Clubs");
    cardlist[44] = Card("7","Clubs");
    cardlist[45] = Card("8","Clubs");
    cardlist[46] = Card("9","Clubs");
    cardlist[47] = Card("10","Clubs");
    cardlist[48] = Card("Jack","Clubs");
    cardlist[49] = Card("Queen","Clubs");
    cardlist[50] = Card("King","Clubs");
    cardlist[51] = Card("Ace","Clubs");

    for (int i = 0; i < 52; i++)
        cardlist[i].print();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您定义了默认构造函数,但您没有定义它:

    Card();

如果您不需要或不需要构造函数,可以使用编译器可以为您生成的默认值:

    Card() = default;

或者只是删除该行。那么你的代码就可以了。

你的错误只是那个。