如何在 C++ 中创建一个作为另一个类的向量的类?
Posted
技术标签:
【中文标题】如何在 C++ 中创建一个作为另一个类的向量的类?【英文标题】:How can I create a class that is a vector of another class in C++? 【发布时间】:2017-11-15 03:40:25 【问题描述】:感谢您阅读我的问题。
我正在尝试开始编写一个使用一副纸牌的程序,因为我想了解如何使用类。我决定创建一个存储套装和值的类,如下所示:
#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;
当我尝试运行代码时出现以下错误:
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.
老实说,我不太确定发生了什么。我非常关注我在网上看到的课程示例。如果有人可以帮助我找到问题,那将不胜感激。抱歉,我对此有点陌生,可能犯了一些愚蠢的错误。非常感谢您花时间和耐心阅读本文并帮助我。
【问题讨论】:
【参考方案1】:你的问题在于_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;
【讨论】:
感谢您的帮助,但它似乎真的不喜欢这样,并且给了我很多错误。还有其他方法可以初始化 _deck 吗? 啊,是 _deck.reserve(51),而不是 deck.resize。谢谢你的技术解释,即使代码是几个字母! 对不起!我已经被Card
中缺少构造函数所挫败。是的,你是对的,虽然不需要调用reserve
,但你确实需要调用push_back
来放置卡片(无论如何)
另外,您可能希望在您的回答中删除甲板后的 [i],因为 _deck[i] 是一张卡片,而不是矢量 _deck,以防其他人遇到此问题。再次感谢您的帮助!
谢谢,我改正了以上是关于如何在 C++ 中创建一个作为另一个类的向量的类?的主要内容,如果未能解决你的问题,请参考以下文章