0

Example of the codeFairly new to object oriented programming. I am trying to make a connect 4 game with a class for the overall game, which contains a board object which then contains two player objects. I've realized that I need my players to be able to access some information from the board, specifically row and column lengths (also using pointers so the user can edit the board size), so I was trying to pass a board object to those specific functions as a parameter. I'm getting the undeclared error stated above but I'm just confused on why I would need to declare it in player class when I'm only using it as a parameter.

For example I get this error: error: 'board' has not been declared 25 | int takeTurn(board& myBoard); | ^~~~~

Here's some of the code from my player.h file:

#ifndef PA02_TEMPLATE_PLAYER_H
#define PA02_TEMPLATE_PLAYER_H


#include "board.h"

class player{
private:
protected:
    char piece;
    int playerMove[2];
    int playerNum;
public:
    player()= default;

    player(char p, int num){
        piece = p;
        playerNum = num;
    }

    int takeTurn(board& myBoard);

    void colChoice(int i);

    void findRow(board& myBoard, int rows);

    void setPlayerNum(int i){
        playerNum = i;
    }
    void setPiece(char p){
        piece = p;
    }

    char getPiece(){
        return piece;
    }

    int getPlayerNum(){
        return playerNum;
    }




};
class aiPlayer : player{
private:
    int aiDifficulty = 2;
public:
    void setDifficulty(int i){
        aiDifficulty = i;
    }
    int getDifficulty(){
        return aiDifficulty;
    }
    void takeTurn(int rows, int COLS, board& myBoard);

    vector<int> minimax(board& miniBoard, int depth, int alpha, int beta, bool maximizingPlayer);
    int scorePosition(char piece, char opponents_piece, board& scoringBoard);
};

#endif //PA02_TEMPLATE_PLAYER_H
ehaug
  • 11
  • [Please do not post images of code because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Code should be posted directly **as text** in your question. – MikeCAT Mar 02 '23 at 18:39
  • Okay thank you! I did also post some code as text as well, I don't know if that helps. – ehaug Mar 02 '23 at 18:40
  • 1
    Is `board` declared in `board.h`? If so please show its contents. – Eugene Mar 02 '23 at 18:42
  • 2
    There is no declaration of `board` in the code you have posted. I guess it's in `board.h` but you haven't posted that. As to why, it's how C++ is everything must be declared before it is used. Maybe the thing you are looking for is a *forward declaration*.. You could forward declare `board` instead of including `board.h`. – john Mar 02 '23 at 18:43
  • 2
    I notice you're referencing `player` in `board` as well as `board` in `player`. You almost certainly have a circular include (i.e. `#include "board.h"` in "player.h" and `#include "player.h"` in "board.h"). That will not work. You'll need to use forward-declarations to break the cycle. – Miles Budnek Mar 02 '23 at 18:48

0 Answers0