为tic-tac-toe构建类(c ++)

时间:2014-03-17 19:23:37

标签: c++ class header-files

我一直在用'C ++编程来学习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;
}

我来自古巴,因此西班牙语中有名称变量。如果您需要,我可以进行全面翻译。 正如我所说的不是整个代码,我没有更多需要的东西

只是为了让一切更清晰我要添加tablero.h这里是casilla被定义.Casilla是一个2D数组

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

1 个答案:

答案 0 :(得分:1)

让我们看看第一个错误。

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