C++洗牌器错误
C++ Card Shuffler Errors
说明是:
创建一个程序来洗牌和发一副牌。该程序应由类卡,类卡组和驱动程序组成。
班级卡应提供:a)数据类型为 int 的数据成员面孔和西装。 b) 一个构造函数,它接收两个表示人脸和套装的整数,并使用它们来初始化数据成员。
类卡片应包含:a)名为 deck 的两个 Card 对象,用于存储两张卡片。 b) 一个不带参数并初始化牌组中两张牌的构造函数。 你可以给这两张牌一个随机的面值和花色值,但要确保它们不一样。 c) 打印两张卡片的打印卡功能。
驱动程序应创建一个 DeckOfCards 对象,并打印此对象具有的卡。
这个项目需要有5个文件:card.hpp,card.cpp,deckofcards.hpp,deckofcards.cpp,main.cpp
我得到的错误是:
- 显式类型在 DeckOfCards.h 第 15 行中缺失(假设为"int")
- 在主.cpp第 8 行中,没有合适的构造函数可以从 "DeckOfCards*" 转换为 "DeckOfCards">
- 缺少类型说明符 -int 假定在 DeckOfCards.h 第 15 行
- "正在初始化":无法在主.cpp第 8 行从"DeckOfCards*"转换为"DeckOfCards">
- 缺少类型说明符 - int 假定在 DeckOfCards.h 第 15 行
- 缺少类型说明符 - 在 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;
在对课程进行编程之前
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'