Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

c++ - I need help using an array with a class

I get an error " undefined reference to 'Card::Card()' ", when I try to compile. This program is supposed to print all cards in a deck after I initialize each one. It has to use OOP so I don't need that changed. Help anyone!

#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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You define that a default constructor exists, but you don't define it:

    Card();

if you don't need or want a constructor, you can use the default one that the compiler can generate for you:

    Card() = default;

Or simply delete that line. your code will work then.

your error is just that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...