我在too many initializers
类文件中收到错误deckofcardsclass.cpp
。我已经看过几个关于这个的帖子,但它们并没有直接关联,而我正在遇到的事情有点复杂,因为我用另一个类的实例填充数组。它只是一个语法错误?或者我的初始化背后的逻辑是不正确的?
要清除:初始化cards_ array时实现文件中发生错误
班级标题
#pragma once
#ifndef DECKOFCARDS_H_
#define DECKOFCARDS_H_
#include <array>
#include "Card.h"
class DeckOfCards
{
public:
DeckOfCards();
void printDeck();
private:
std::array<Card, 52> cards_;
};
#endif // !1
班级实施
#include "stdafx.h"
#include <iostream>
#include "DeckOfCards.h"
#include "Card.h"
#include <array>
DeckOfCards::DeckOfCards()
:
cards_
{
{ Card::Diamonds, Card::Two },
{ Card::Diamonds, Card::Three },
{ Card::Diamonds, Card::Four },
{ Card::Diamonds, Card::Five },
{ Card::Diamonds, Card::Six },
{ Card::Diamonds, Card::Seven },
{ Card::Diamonds, Card::Eight },
{ Card::Diamonds, Card::Nine },
{ Card::Diamonds, Card::Ten },
{ Card::Diamonds, Card::Jack },
{ Card::Diamonds, Card::Queen },
{ Card::Diamonds, Card::King },
{ Card::Diamonds, Card::Ace },
{ Card::Hearts, Card::Two },
{ Card::Hearts, Card::Three },
{ Card::Hearts, Card::Four },
{ Card::Hearts, Card::Five },
{ Card::Hearts, Card::Six },
{ Card::Hearts, Card::Seven },
{ Card::Hearts, Card::Eight },
{ Card::Hearts, Card::Nine },
{ Card::Hearts, Card::Ten },
{ Card::Hearts, Card::Jack },
{ Card::Hearts, Card::Queen },
{ Card::Hearts, Card::King },
{ Card::Hearts, Card::Ace },
{ Card::Spades, Card::Two },
{ Card::Spades, Card::Three },
{ Card::Spades, Card::Four },
{ Card::Spades, Card::Five },
{ Card::Spades, Card::Six },
{ Card::Spades, Card::Seven },
{ Card::Spades, Card::Eight },
{ Card::Spades, Card::Nine },
{ Card::Spades, Card::Ten },
{ Card::Spades, Card::Jack },
{ Card::Spades, Card::Queen },
{ Card::Spades, Card::King },
{ Card::Spades, Card::Ace },
{ Card::Clubs, Card::Two },
{ Card::Clubs, Card::Three },
{ Card::Clubs, Card::Four },
{ Card::Clubs, Card::Five },
{ Card::Clubs, Card::Six },
{ Card::Clubs, Card::Seven },
{ Card::Clubs, Card::Eight },
{ Card::Clubs, Card::Nine },
{ Card::Clubs, Card::Ten },
{ Card::Clubs, Card::Jack },
{ Card::Clubs, Card::Queen },
{ Card::Clubs, Card::King },
{ Card::Clubs, Card::Ace } }
{}
void DeckOfCards::printDeck()
{
bool first = true;
for (auto card : cards_)
{
if (!first)
{
std::cout << ", ";
}
card.printCard();
first = false;
}
}
卡片标题
#pragma once
#ifndef CARD_H_
#define CARD_H_
struct Card
{
enum Suit_Type
{
Diamonds,
Hearts,
Spades,
Clubs,
} suit;
enum Value_Type
{
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
Ace = 14
} value;
void printCard();
};
#endif // !CARD_H
_
答案 0 :(得分:0)
你可能会发现这会拉伸你,但是在填充数组或向量的地方可以用算法术语来描述,我可能会这样做。
在这种情况下,我使用constexpr生成数组,因为std::array
支持constexpr
构造。如果您决定使用矢量,类似的方法可行,但您需要删除constexpr
。
#include <array>
#include <iostream>
#include <utility>
#include <random>
#include <algorithm>
struct Card
{
enum Suit_Type
{
Diamonds,
Hearts,
Spades,
Clubs,
} suit;
friend std::ostream& operator<<(std::ostream& os, Suit_Type s) {
switch (s) {
case Card::Diamonds: return os << "Diamonds";
case Card::Hearts: return os << "Hearts";
case Card::Spades: return os << "Spades";
case Card::Clubs: return os << "Clubs";
}
}
enum Value_Type
{
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
Ace = 14
} value;
friend std::ostream& operator<<(std::ostream& os, Value_Type s) {
switch (s) {
case Card::Two: return os << "Two";
case Card::Three: return os << "Three";
case Card::Four: return os << "Four";
case Card::Five: return os << "Five";
case Card::Six: return os << "Six";
case Card::Seven: return os << "Seven";
case Card::Eight: return os << "Eight";
case Card::Nine: return os << "Nine";
case Card::Ten: return os << "Ten";
case Card::Jack: return os << "Jack";
case Card::Queen: return os << "Queen";
case Card::King: return os << "King";
case Card::Ace: return os << "Ace";
}
}
void printCard();
};
void Card::printCard() {
std::cout << value << " of " << suit;
}
constexpr Card card_from_index(int index) {
return {
static_cast<Card::Suit_Type>(index / 13),
static_cast<Card::Value_Type>((index % 13) + 2)
};
}
class DeckOfCards
{
public:
DeckOfCards();
void printDeck();
template<class Engine>
void shuffle(Engine&& eng) {
std::shuffle(std::begin(cards_), std::end(cards_),
std::forward<Engine>(eng));
}
private:
static constexpr int nof_cards = 52;
using deck_store = std::array<Card, nof_cards>;
template<int...Is>
constexpr static deck_store generate_deck(std::integer_sequence<int, Is...>)
{
deck_store result{ card_from_index(Is)... };
return result;
}
private:
deck_store cards_;
};
DeckOfCards::DeckOfCards()
: cards_ (generate_deck(std::make_integer_sequence<int, nof_cards>()))
{}
void DeckOfCards::printDeck()
{
bool first = true;
for (auto card : cards_)
{
if (!first)
{
std::cout << ", ";
}
card.printCard();
first = false;
}
std::cout << "\n";
}
int main()
{
DeckOfCards deck;
deck.printDeck();
deck.shuffle(std::default_random_engine { std::random_device {} ()});
std::cout << "\nafter shuffling:\n\n";
deck.printDeck();
}