【问题标题】:Error thrown about types in header file C++关于头文件 C++ 中的类型引发的错误
【发布时间】:2019-12-15 19:32:42
【问题描述】:

我使用的是与内置矢量类型一起创建的对象。当我将它们设置为成员函数中的参数时,编译器会抛出一个抱怨变量类型的错误。我不确定为什么编译器会通过突出显示它们将它们识别为对象,所以我不确定错误是什么。

发生错误的地方

#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);
};

humanPlayer.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);

     };

humanPlayer.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);
    }
}

【问题讨论】:

  • 1) 请在您的问题代码中发布错误,而不是它们的图像。 2)请正确格式化您的代码。 3)您的代码没有包含足够的信息。错误基本上是说humanPlayer 不存在,我认为这是真的,因为我在任何地方都看不到它。 4) 使用vector时,指定其全名std::vector
  • 请提供minimal reproducible example,估计您有错字或循环包含
  • 避免发布错误信息的图片。太多用户无法看到图像。在错误列表选项卡附近,您应该找到输出选项卡。输出选项卡将以文本形式显示完整的构建输出,使粘贴到 Stack Overflow 问题中变得非常容易。作为额外的好处,构建输出通常会提供更多信息和上下文,从而使错误更容易解决。
  • 如果您向我们显示“humanPlayer.h”,也许我们可以追踪错误。错误可能来自那里。
  • 我找不到错误报告的输出选项卡。我用更多可能包含错误的代码更新了问题

标签: c++ visual-c++ syntax compiler-errors


【解决方案1】:

你有一个头文件的循环引用。 Game.h 包括 humanPlayer.h,其中包括 Game.h。这是不行的。在您的情况下,您可以简单地从您的 humanPlayer.h 文件中删除 #include "Game.h"

在头文件中,只包含直接需要的头文件。除非您打算明确使用这些类/函数,否则不要包含头文件。

在不相关的注释上,please read this about namespace std

【讨论】:

  • 头文件的循环依赖并不少见,而且非常好。标头防护可防止无限循环。
  • @TedLyngmo,OP 确实有保护(以 pragma 的形式)......我不知道有任何保护可以防止标题之间的循环引用;不使用前向声明。你能启发我吗?
  • 标头保护防止同一个标头在一个翻译单元中多次包含,无论是否前向声明。在 OP:s 代码中,包括来自 humanPlayer.hgame.h 是不必要的,但我看不出它可能会导致什么问题。
  • 哦,我明白你的意思了。我想哪个编译说humanPlayer.cpp,它包括humanPlayer.h,然后包括Game.h。在Game.h 的包含点,humanPlayer 没有定义,所以发生了错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2014-03-14
  • 1970-01-01
相关资源
最近更新 更多