我收到此错误50:23: error: ‘d.Deck<T>::pullCard [with T = Card]’ does not have class type
我对C ++很陌生。第50行是对main()中d.pullCard的调用。尝试使用我的Java知识来实现这一点,但这种语言看起来要严格得多。
blackjack.cpp
enum Suite { S, H, C, D };
string symbols[] = { "♠", "♥", "♣", "♦" };
class Card
{
Suite suite;
int value;
public:
Card(Suite s, int v) { suite = s; value = v; }
Suite getSuite() { return suite; }
int getValue() { return value; }
string toString() { return symbols[suite] + " " + to_string(value); }
};
template <typename T>
class Deck : public vector<Card>{
public:
Deck(){
for (int i = 0; i < 13; i++) push_back(Card(S, i+1));
for (int i = 0; i < 13; i++) push_back(Card(H, i+1));
for (int i = 0; i < 13; i++) push_back(Card(C, i+1));
for (int i = 0; i < 13; i++) push_back(Card(D, i+1));
}
void shuffle(){
srand(unsigned(time(0)));
random_shuffle(begin(), end());
}
Card pullCard(){
int index = rand() % 52 + 1;
Card r = this[index];
erase(index);
return r;
}
};
int main(){
Deck<Card> d;
d.shuffle();
for (int i = 0; i < d.size(); i++) cout << d[i].toString() << endl;
cout << d.pullCard.toString();
}
答案 0 :(得分:2)
您没有调用pullCard
函数。你需要改变这个:
d.pullCard.toString();
到此:
d.pullCard().toString();