gpt4 book ai didi

c++ - 'myMethod' 没有类类型

转载 作者:行者123 更新时间:2023-11-30 03:57:02 25 4
gpt4 key购买 nike

我收到此错误 50:23: error: ‘d.Deck<T>::pullCard [with T = Card]’ does not have class type

我是 C++ 的新手。第 50 行是在 main() 中对 d.pullCard 的调用。尝试使用我的 Java 知识来做到这一点,但这种语言似乎更加严格。

二十一点.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();
}

最佳答案

您没有调用 pullCard 函数。你需要改变这个:

d.pullCard.toString();

为此:

d.pullCard().toString();

关于c++ - 'myMethod' 没有类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28232618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com