0

I was using objects which I create along with the built-in vector type. The compiler is throwing an error complaining about the type of the variables as I am setting them as parameters in the member functions. I am not sure why as the compiler recognizes them as objects by highlighting them, so I am unsure what the error is.

Where the error is occuring

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

Error Report

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Minthew67
  • 1
  • 1
  • 3
    1) Please post the errors in your question code, not an image of them. 2) Please format your code properly. 3) Your code does not contain enough information. The errors are basically saying `humanPlayer` does not exist, which I will assume is true because I don't see it anywhere. 4) When using `vector`, specify its fully qualified name of `std::vector` – ChrisMM Dec 15 '19 at 19:35
  • 1
    Please provide a [mre], at a guess you have a typo or circular includes – Alan Birtles Dec 15 '19 at 19:35
  • 2
    Avoid posting images of error messages. Images cannot be seen by too many users. Down near the Error List tab you should find the Output tab. The Output tab will show you the complete build output in text, making it ludicrously easy to paste into a Stack Overflow question. As an added bonus, the build output often provides much more information and context, making the error easier to solve. – user4581301 Dec 15 '19 at 19:36
  • Maybe we can track down the error, if you show us "humanPlayer.h". Probably the error comes from there. – Lukas-T Dec 15 '19 at 19:43
  • I could not find the output tab for the error report. I updated the question with more code that may hold the error – Minthew67 Dec 15 '19 at 19:51
  • Unrelated, but never do `using namespace std;` in header files. – Ted Lyngmo Dec 15 '19 at 19:58

1 Answers1

1

You have a circular reference of header files. Game.h includes humanPlayer.h which includes Game.h. This is a no no. In your case, you can simply remove the #include "Game.h" from your humanPlayer.h file.

In header files, only include the header files which are directly required. Do not include header files unless you are planning to use those classes/functions expressly.

On an unrelated note, please read this about namespace std.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • Circular dependencies when it comes to header files is not uncommon and is perfectly fine. Header guards protect against an endless loop. – Ted Lyngmo Dec 15 '19 at 20:02
  • @TedLyngmo, OP did have the guards (in the form of pragma) … I'm not aware of any guard which can protect against circular references between headers; without using forward declarations. Could you enlighten me? – ChrisMM Dec 15 '19 at 20:50
  • The header guard protects against the same header being included more than once in one translation unit, forward declarations or not. In OP:s code, including `game.h` from `humanPlayer.h` was unnecessary, but I don't see what problem it could cause having it there. – Ted Lyngmo Dec 15 '19 at 20:56
  • 1
    Oh, I see what you mean now. I imagine which compiling say `humanPlayer.cpp`, it included `humanPlayer.h`, which then included `Game.h`. At the point of `Game.h`'s inclusion, `humanPlayer` was not defined, so the error occurred. – ChrisMM Dec 15 '19 at 21:00