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

标签 c++

我收到此错误 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/

相关文章:

c++ - 更快的 JsonCpp 替代方案允许 Json 对象的复制/可变性?

c++ - 对 `tesseract::TessBaseAPI::TessBaseAPI()' 的 undefined reference

大型 vector/数组的 C++ 未处理异常

c++ - 写static const uint变量和匿名枚举变量有什么区别?

c++ - 如何在文件打开对话框中隐藏特定控件? (使用 CFileDialog 创建的对话框)

c++ - 使 autoconf 搜索 C++ 库

c++ - 自定义类的 Cin 参数

c++ - 为什么 map<string, string> 接受整数作为值?

c++ - 在 C++ 中,如何在一个参数中测试与 "|"组合的多个标志之一?

c++ - 递归函数中的段错误