c++ - 即使在 header 中,另一个类也无法识别的类

标签 c++ class

我必须制作带有类(MyGame,Board,Player,Dice)的蛇和梯子游戏。 MyGame在某个时候需要所有其他类,因此MyGame.h文件中有其他类的头文件。但是我得到3个错误,内容为:
第18行-----“错误:'Board'尚未声明。”
第18行-----“错误:尚未声明'Player'。”
第19行-----“错误:尚未声明'Player'。”

我的主对象(skanes.cpp)中初始化了一个对象MyGame,然后在函数MyGame::start()中创建了其他对象。我认为也许Board或Player类需要MyGame的某些东西才能进行构建,因此循环使用,但是Player和Board除了对象的初始化外不依赖MyGame。帮帮我!

MyGame.h

#ifndef MYGAME_H
#define MYGAME_H
#include "Board.h"
#include "Player.h"
#include "Dice.h"

#include "Player.h"
class MyGame
{

    protected:
        static const int numPlayers = 2;

    public:
        MyGame();
        ~MyGame();
        void start();
        void play(Player[], Dice, Board);  <-------Line 18
        void win(Player[]);                <-------Line 19
        int getNumPLayers();
};

#endif

MyGame.cpp
#include <iostream>
#include <vector>
#include "MyGame.h"
#include "Board.h"
#include "Player.h"
#include "Dice.h"

MyGame::MyGame()
{

}

MyGame::~MyGame()
{

}

void MyGame::start()
{
    Board brd;
    Player plyr[numPlayers];
    Dice dc;
    while (plyr[0].getPosition() != brd.getBoardSize() && plyr[1].getPosition() != brd.getBoardSize() && plyr[numPlayers - 1].getTurn() <= plyr[numPlayers - 1].getMaxTurn())
        play(plyr, dc, brd);

    win(plyr);

}

void MyGame::play(Player p[], Dice d, Board b)
{

        for (int i = 0; i < b.getBoardSize(); i++)
        {
            p[i].setPosition(d.roll());
            if(p[i].getPosition() > b.getBoardSize())
            {
                p[i].setPosition( (b.getBoardSize() - p[i].getPosition()) * 2 );
            }

            if (b.getType(p[i].getPosition()) == 'S')
                p[i].setPosition(-b.getSnakeLadderMove());
            else if (!b.getType(p[i].getPosition()) == 'L')
                p[i].setPosition(b.getSnakeLadderMove());

            p[i].setTurn();
        }

}

void MyGame::win(Player p[])
{
    for (int i = 0; i > numPlayers; i++) 
    {
        if (p[i].getPosition() == 30)
            std::cout << "Payer " << i << "wins!!" << std::endl;
    }
}

Board.h
#ifndef BOARD_H
#define BOARD_H
#include "MyGame.h"

class Board
{
    public:
        Board();
        ~Board();
        bool getType(int);
        int getNumeber(int);
        int getSnakeLadderMove();
        int getBoardSize();

    private:
        struct tile
        {
            char type;
            int number;
        };

        static const int boardSize = 30;
        static const int snakeLadderMove  = 3;
        tile place[boardSize];
};

#endif

Board.cpp
#include <stdlib.h>
#include <time.h>
#include "Board.h"

Board::Board()
{
    int count = 0;
    //initialize random seed to randomize snakes and ladders.
    srand(time(NULL));

    for (int k = 0; k < boardSize; k++)
    {
        place[k].type = 'N';
        place[k].number = k + 1;
    }

    while(count <= 3)
    {
        int index = rand() % boardSize + 1;

        while (index < 4)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N'
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }

        place[index].type = 'S';

        while (index > boardSize - 3)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N' 
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }   

        place[index].type = 'L';
        count++;

    }
}

Board::~Board()
{

}

int Board::getNumeber(int index)
{
    return place[index].number;
}

bool Board::getType(int index)
{
    return place[index].type;
}

int Board::getBoardSize()
{
    return boardSize;
}

Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"
#include "Board.h"

class Player
{
    public:
        Player();
        ~Player();
        void setPosition(int);
        void setTurn();
        int getPosition();
        int getTurn();
        int getMaxTurn();
        int getNumPlayers();

    private:
        static const int maxTurn = 20;
        int position;
        int turn;
};

#endif

Player.cpp
#include <iostream>
#include "Player.h"
#include "Board.h"

Player::Player()
{

    /* 
        In order for the setters to work position and turn
        have to be equal to 1;
    */
    position = 1;
    turn = 1;
}

Player::~Player()
{

}

void Player::setPosition(int move)
{
    //Assumes constructor setted the value to 0
    position += move;
    ;
}

void Player::setTurn()
{
    //Assumes constructor sette4d the value to 0
    turn++;
}


int Player::getPosition()
{
    return position;
}

int Player::getTurn()
{
    return turn;
}

int Player::getMaxTurn()
{
    return maxTurn;
}

Dice.h
#ifndef CDADO_H_INCLUDED
#define CDADO_H_INCLUDED

#include <ctime>
#include <cstdlib>

class Dice{

    public:

        Dice();
        int roll();

};


#endif

Dice.cpp
#include <iostream>
#include "Dice.h"

using namespace std;


Dice::Dice()
{
    srand(time(0));
}


int Dice::roll()
{
    return  (rand() % 6) + 1;
}  

skanes.cpp //应该是蛇。
#include <iostream>
#include "MyGame.h"

using namespace std;

int main()
{
    MyGame snakes;

    snakes.start();
}

最佳答案

#define BOARD_H
#include "MyGame.h"  // <--- here lies the problem

不要在MyGame.hBoard.h中包括Player.h。您具有循环依赖关系。

关于c++ - 即使在 header 中,另一个类也无法识别的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926298/

相关文章:

java - 如果提供了某些参数,是否可以禁止创建类对象?

c++ - 使用指针 vector 的问题(附代码)C++(以及对其使用便利性的怀疑)

c++ - 如何解决 "read of non-constexpr variable ' a' is not allowed in a constant expression"with boost.hana

c++全局变量超出类范围

java - 尝试从 Delphi 调用基于 Android JAR 的 JAVA 类时缺少类错误

java - Java 中泛型类型的行为

python - Boost-Python C++ 项目构建,如何使用 Python 中的新库?

c++ - C/C++ 最有效的 if 语句评估

c++ - 开罗矩阵与 GlOrtho 矩阵等效?

javascript - 如何在类中创建元素创建方法