简体   繁体   English

井字棋(c++)的结构化类

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

I have been learning c++ with 'C++ Programming for the Absolute Beginner' which have been very useful, whoever when it comes to OOP and splitting the classes into different files it doesn't explain it very well.我一直在通过“面向绝对初学者的 C++ 编程”来学习 C++,这本书非常有用,无论是谁在 OOP 和将类拆分为不同的文件时,它都不能很好地解释它。

This is the code I have, I didn't put everything here, just the problematic stuff.这是我的代码,我没有把所有东西都放在这里,只是有问题的东西。 The weird thing is that if I exclude Juego.h and Juego.cpp from the project it lets me build it, but if I include it I get the following errors:奇怪的是,如果我从项目中排除 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'

Because it only happens with Juego.h in the code I think the problem is that I didn't structure well the header files.因为它只发生在代码中的 Juego.h 我认为问题是我没有很好地构建头文件。 I have been able to solve many of the problems searching on google but I can't fix those, I even get syntax errors.我已经能够解决在 google 上搜索的许多问题,但我无法解决这些问题,甚至出现语法错误。

//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;
}

As I said it isn't the whole code, I wasnt to have more things that needed just to make everything clearer I'm gonna add tablero.h正如我所说这不是整个代码,我没有更多的东西只是为了让一切更清晰我要添加 tablero.h

Here is were casilla is defined.这里定义了 casilla。 Casilla is a 2D array: 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";
        }
    }
}
}

Let us look at the first error.让我们看看第一个错误。

//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;

At this moment, the compiler does not know what a casilla is.此时,编译器不知道 casilla 是什么。 Perhaps it is in some other file which has to be included, too, before any use of it:也许在使用它之前,它也必须包含在其他文件中:

//Jugador.h
#include <string>
#include <where-casilla-is-from>
using std::string;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM