构造井字棋的类(c++)

structuring classes for a tic-tac-toe(c++)

本文关键字:c++ 造井      更新时间:2023-10-16

我一直在用' c++ Programming '学习c++《绝对初学者》这本书非常有用,但是当涉及到OOP和将类拆分到不同的文件中时,它并没有很好地解释它。

这是我的代码,我没有把所有的东西都放在这里,只是有问题的东西。奇怪的是,如果我从项目中排除Juego.h和Juego.cpp,它允许我构建它,但如果我包含它,我得到以下错误:

jugador.h(8): error C2146: syntax error : missing ';' before identifier 'c_o'
jugador.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
jugador.h(9): error C2061: syntax error : identifier 'casilla'
juego.cpp(15): error C2039: 'c_o' : is not a member of 'Jugador'    
jugador.h(5) : see declaration of 'Jugador'

因为它只发生在Juego.h代码中,我认为问题是我没有很好地构建头文件。我已经能够解决许多问题在谷歌搜索,但我不能修复那些,我甚至得到语法错误。

//Jugador.h
#include <string>
using std::string;
class Jugador
{
public:
    string p_name;
//casilla is X,O or blankspace, done using enum in Tablero.h( it have to be in a cpp file)
    casilla c_o;
Player(string name, casilla marca);
void turno(Tablero* tabla);
};

//Jugador.cpp
#include "Jugador.h"
#inlcude "Tablero.h"
#include <string>
using std::string;
Jugador::Jugador(string nombre,casilla marca): p_name(nombre), c_o(marca) {} 
void Jugador::turno(Tablero* tabla)
{
using std::cout;
using std::cin;
int fil;
int col;
do
{
    cout << p_name.c_str() <<" en que fila quieres jugar(1-2-3)?n";
    cin >> fil;
    cout<<p_name.c_str()  <<" en que columna quieres jugar(1-2-3)?n";
    cin >> col;
}while(tabla->tab[fil-1][col-1]==vacia);
tabla->tab[fil-1][col-1]=c_o;
}

//Juego.h
class Jugador;
class Tablero;
class Juego
{
public:
Juego(void);
bool ganador(Jugador* player, Tablero* tabla);
bool fin(Tablero* tabla);
};

//Juego.cpp
#include "Juego.h"
#include "Jugador.h"
#include "Tablero.h"
Juego::Juego() {}
bool Juego::ganador(Jugador* player, Tablero* tabla)
{
using std::cout;
using std::cin;

casilla marca_jug = player->c_o;
bool winner = false;
    //...
// if-else structure which set winner to true if the conditons to win are achived       
if (winner)
    cout<<player->p_name.c_str()<<" ha ganado!!!!n";
return winner;
}
//if there isnt any blank square, ends the game
bool Juego::fin(Tablero* tabla)
{
bool fin_juego=false;
for(int fil=0; fil<3; fil++)
{
    for(int col=0; col<3; col++)
    {
        if(tabla->tab[fil][col]==vacia)
            fin_juego=true;
    }
}
return fin_juego;
}

就像我说的,这不是全部代码,我不想有更多的东西只是为了让一切更清晰我要添加tableo。h

这里是casilla的定义。Casilla是一个二维数组:

//Tablero.h
#include <iostream>
#include <string>
enum casilla {vacia,X,O};
class Tablero
{
public:
casilla tab[3][3];
Tablero(void);
void draw(void);
};

//Tablero.cpp
#include "Tablero.h"
Tablero::Tablero(void)
{
using std::cout;
casilla tab[3][3]; 
for(int f=0;f<3;f++)
{
    for (int c= 0;c<3;c++)
    {
        tab[f][c]=vacia;
    }
}
}
void Tablero::draw(void)
{
using std::cout;
using std::string;
for(int f=0;f<3;f++)
{
    for (int c= 0;c<3;c++)
    {
        string s;
        if (c!=2)
        {
            switch (tab[f][c])
            {
                case vacia: s=" "; break;
                case X: s="X"; break;
                case O: s="O";break;
            }
            cout << s.c_str() <<" | ";
        }
        else 
        {   
            switch (tab[f][c])
            {
                case vacia: s=" "; break;
                case X: s="X"; break;
                case O: s="O"; break;
            }
            cout << s.c_str() <<"n";
        }
    }
}
}

让我们看看第一个错误。

//Jugador.h
#include <string>
using std::string;
class Jugador
{
public:
    string p_name;
//casilla is X,O or blankspace, done using enum in Tablero.h( it have to be in a cpp file)
    casilla c_o;
此时,编译器不知道casilla是什么。也许它在其他文件中,在使用它之前也必须包含它:
//Jugador.h
#include <string>
#include <where-casilla-is-from>
using std::string;
...