C++洗牌器错误

C++ Card Shuffler Errors

本文关键字:错误 C++      更新时间:2023-10-16

说明是:

创建一个程序来洗牌和发一副牌。该程序应由类卡,类卡组和驱动程序组成。

班级卡应提供:a)数据类型为 int 的数据成员面孔和西装。 b) 一个构造函数,它接收两个表示人脸和套装的整数,并使用它们来初始化数据成员。

类卡片应包含:a)名为 deck 的两个 Card 对象,用于存储两张卡片。 b) 一个不带参数并初始化牌组中两张牌的构造函数。 你可以给这两张牌一个随机的面值和花色值,但要确保它们不一样。 c) 打印两张卡片的打印卡功能。

驱动程序应创建一个 DeckOfCards 对象,并打印此对象具有的卡。

这个项目需要有5个文件:card.hpp,card.cpp,deckofcards.hpp,deckofcards.cpp,main.cpp

我得到的错误是:

  1. 显式类型在 DeckOfCards.h 第 15 行中缺失(假设为"int")
    1. 在主.cpp第 8 行中,没有合适的构造函数可以从 "DeckOfCards*" 转换为 "DeckOfCards">
    2. 缺少类型说明符 -int 假定在 DeckOfCards.h 第 15 行
    3. "正在初始化":无法在主.cpp第 8 行从"DeckOfCards*"转换为"DeckOfCards">
    4. 缺少类型说明符 - int 假定在 DeckOfCards.h 第 15 行
    5. 缺少类型说明符 - 在 DeckOfCards.cpp第 18 行中假定的整数

卡:

#ifndef Card_H
#define Card_H
#include <string>
using namespace std;

string suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
string faces[12] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "Jack", "Queen", "King" };
class Card {
public:
int face;
int suit;
Card(int face, int suit);
string toString();
};
#endif

卡.cpp:

#include "Card.h"
#include <iostream>
using namespace std;
Card::Card(int face, int suit) {
this->face = face;
this->suit = suit;
}
string Card::toString() {
string suitname = suits[suit];
string facename = faces[face];
return facename + " of " + suitname;
}

DeckOfCards.h:

#ifndef DeckOfCards_H
#define DeckOfCards_H
#include "Card.h"
#include <vector>
using namespace std;

class DeckOfCards {
public:
vector<Card> deck;
public:
DeckOfCards();
printCards();
};
#endif

纸牌组.cpp:

#include "DeckofCards.h"
#include "Card.h"
#include <iostream>
using std::cin;
using std::cout;
using std::string;

DeckOfCards::DeckOfCards() {
for (int i = 0; i<2; i++) {
Card card(i + 3, i + 5);
deck.push_back(card);
}
}
DeckOfCards::printCards() {
for (int i = 0; i<2; i++) {
cout << deck[i].toString();
}
}

主.cpp:

#include <iostream>
#include "Card.h"
#include "DeckOfCards.h"
using namespace std;
int main() {
DeckOfCards cardDeck = new DeckOfCards();
cardDeck.printCards();
return 0;
}

要使用 std::string 类,您必须包含头文件字符串,因此要将其导入相应的 hpp 文件,您需要编写此

内容
#include <string>

现在,您可以添加 std::string 而不是总是键入

using std::string;

在对课程进行编程之前