c++ - 关于头文件 C++ 中的类型引发的错误
问题描述
我正在使用与内置矢量类型一起创建的对象。当我将它们设置为成员函数中的参数时,编译器会抛出一个抱怨变量类型的错误。我不确定为什么编译器会通过突出显示它们将它们识别为对象,所以我不确定错误是什么。
发生错误的地方
#pragma once
#include "board.h"
#include "player.h"
#include "humanPlayer.h"
#include <vector>
class Game {
private:
int winner;
int X;
int Y;
bool** playerShip;
bool** computerShip;
public:
Game(int x, int y);
void startGame();
void WinScreen();
void LossScreen();
void ByeScreen();
void gameSetUpE(Board& B, humanPlayer User);
void gameSetUpS(Board& B, humanPlayer User);
void gameSetUpH(Board& B, humanPlayer User);
int playGame(Board& B, humanPlayer P1, Player P2);
char anotherGame();
void endGame(int num, Board& B);
void setWinner(int play) { winner = play; };
void setRecords(Board& B);
int getWinner() { return winner; };
bool getUserRecordVal(int row, int col);
bool getCompRecordVal(int row, int col);
int getRecordX() { }
void endGame(vector<Game> &list);
};
人类播放器.h
#pragma once
#include "board.h"
#include "game.h"
#include <iostream>
using namespace std;
class humanPlayer : public Player{
private:
int hitsGotten;
int winHits;
int score;
public:
humanPlayer(int hits);
void makePlayerShip(Board& B, int length);
};
人类播放器.cpp
#include "humanPlayer.h"
humanPlayer::humanPlayer(int hits){
winHits = hits;
hitsGotten = 0;
score = 0;
}
void humanPlayer::makePlayerShip(Board& B, int length) {
int startRow;
int startCol;
char Direction;
int Dint{};
int rowNum = B.getDimY();
int colNum = B.getDimX();
cout << " Ship Length " << length << endl;
cout << "Choose a row number between 1 and " << rowNum << endl;
cin >> startRow;
cout << "Choose a column number between 1 and " << colNum << endl;
cin >> startCol;
cout << "Choose a Direction (Horizontal(H) or Vertical(V))" << endl;
cin >> Direction;
if (toupper(Direction) == 'H') {
Dint = 0;
}
else if (toupper(Direction) == 'V') {
Dint = 1;
}
if ((startRow < 1) || (startCol > B.getDimY())) {
cout << "Starting row is out of range" << endl;
cout << "Choose a row number between 1 and " << rowNum << endl;
cin >> startRow;
}
if ((startCol < 1) || (startCol > B.getDimX())) {
cout << "Starting columns is out of range" << endl;
cout << "Choose a column number between 1 and " << colNum << endl;
cin >> startCol;
}
startCol = startCol - 1;
startRow = startRow - 1;
if (Dint == 0) {
int res0 = B.makeShipH(length, startRow, startCol);
if (res0 == 0) {
makePlayerShip(B, length);
}
}
else if (Dint == 1) {
int res1 = B.makeShipV(length, startRow, startCol);
if (res1 == 0) {
makePlayerShip(B, length);
}
}
else {
cout << "Please Try again" << endl;
makePlayerShip(B, length);
}
}
解决方案
您有头文件的循环引用。Game.h
包括humanPlayer.h
其中包括Game.h
。这是不行的。在你的情况下,你可以简单地#include "Game.h"
从你的humanPlayer.h
文件中删除。
在头文件中,只包含直接需要的头文件。除非您打算明确使用这些类/函数,否则不要包含头文件。
在不相关的说明中,请阅读有关命名空间 std的内容。
推荐阅读
- css - wenzhixin如何操作vue-bootstrap-table
- jsf - 如何将大文件从 javascript 发送到 jsf bean
- java - 除数返回无穷大
- r - 如何解释看起来不寻常的障碍模型的结果?
- go - Go 项目目录结构中如何组织和分解服务?
- python-3.x - 如何调试用 Python 编写的未启动的 Cumulocity 微服务?
- reactjs - 更好地理解 API 请求的 debounceTime (RXJS)
- javascript - 将值从数据库传递到 JS React 中的翻转卡片
- c# - 如何以编程方式设置 ASP.NET Chart OnClick 事件
- emacs - 从组织日历自定义日记模板插入