如何在C ++中创建另一个类的向量的类?

时间:2017-11-15 03:40:26

标签: c++ class vector playing-cards

感谢您阅读我的问题。

我试图开始编写一个使用一副牌的程序,因为我想学习使用类。我决定创建一个存储套装和值的类,如下所示:

#ifndef CARD_H
#define CARD_H

class Card {        
    public:
        //here are the individual card values
        int _suit;
        int _value;
        Card(int suit, int value);
};

#endif
#include "card.h"

//define suits
const int spades = 0;
const int clubs = 1;
const int hearts = 2;
const int diamonds = 3;
//define face cards
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;

Card::Card(int suit, int value){
    _suit = suit;
    _value = value;
}

然后我决定创建一个甲板类,并在该类中通过分配0-3(套装)中的值然后从2-14(卡的排名)中分配值来初始化我的卡。我还将卡片添加到我定义为持有它们的矢量中,如下所示:

#ifndef DECK_H
#define DECK_H

#include <vector>

class Deck {
    public:
        std::vector<Card> _deck(51);
        Deck();
};

#endif
#include <iostream>
#include <vector>

#include "card.h"
#include "deck.h"

const int all_suits = 4 - 1;
const int all_values = 14;

Deck::Deck(){
    int i = 0;
    for (int j = 0; j <= all_suits; ++j) {
        //begin with 2 because card can't have value 0
        for (int k = 2; k <= all_values; ++k) {
            _deck[i] = Card(j, k);
            ++i;
        }
    }
}   

当我尝试像这样测试程序时,这会给我一些问题:

#include <iostream>
#include <vector>

#include "card.h"
#include "deck.h"

int main() {
    Deck new_deck = Deck();
    std::cout << new_deck._deck[4]._value;
  return 0;
}

当我尝试运行代码时,会出现以下错误:

&#13;
&#13;
In file included from main.cpp:6:
./deck.h:8:27: error: expected parameter declarator
                std::vector<Card> _deck(51);
                                        ^
./deck.h:8:27: error: expected ')'
./deck.h:8:26: note: to match this '('
                std::vector<Card> _deck(51);
                                       ^
main.cpp:22:24: error: reference to non-static member function must be called; did you mean to call it with no arguments?
        std::cout << new_deck._deck[4]._value;
                     ~~~~~~~~~^~~~~
                                   ()
3 errors generated.
In file included from deck.cpp:8:
./deck.h:8:27: error: expected parameter declarator
                std::vector<Card> _deck(51);
                                        ^
./deck.h:8:27: error: expected ')'
./deck.h:8:26: note: to match this '('
                std::vector<Card> _deck(51);
                                       ^
deck.cpp:18:4: error: reference to non-static member function must be called; did you mean to call it with no arguments?
                        _deck[i] = Card(j, k);
                        ^~~~~
                             ()
3 errors generated.
&#13;
&#13;
&#13;

老实说,我不太确定发生了什么。我非常密切地关注了我在网上看到的课程示例。如果有人能帮助我找到问题,那将非常感激。对不起,我对此有点新意,可能犯了一些愚蠢的错误。我真的很感激你的时间和耐心,阅读并帮助我。

1 个答案:

答案 0 :(得分:3)

您的问题在于_deck

的声明
class Deck {
    public:
        std::vector<Card> _deck(51);

在这里,您尝试声明_deck并调用构造函数,但不能从类成员初始化器中执行此操作。对于没有构造函数的数据成员,可以这样做,例如:

class Deck {
    public:
        int no_of_cards = 51;
        std::vector<Card> _deck;

您需要使用构造函数初始化_deck(就像您已经在做的那样:

Deck::Deck() {
    _deck.reserve(51);
    int i = 0;
    for (int j = 0; j <= all_suits; ++j) {
        //begin with 2 because card can't have value 0
        for (int k = 2; k <= all_values; ++k) {
            _deck.push_back(Card(j, k));
            ++i;
        }
    }
}

并将_deck的声明留作:

class Deck {
    public:
        std::vector<Card> _deck;