构造井字棋的类(c++)
structuring classes for a tic-tac-toe(c++)
我一直在用' 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;
...
相关文章:
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- 井字游戏具有奇怪行为的最小最大值算法(C++)
- 井字游戏:评估节点的启发式值
- 井字C++覆盖板
- 井字游戏:人类与计算机
- 井字棋板,其尺寸基于用户输入
- 如何使用 std::multimap 将整数键映射到两个用作多维数组坐标的整数值(对于井字)?
- 在井字游戏中为平局游戏制作函数
- 井字游戏中的错误
- 我如何重置我的井字游戏的值
- 井字游戏 c++ Agrid 不会重置
- 需要井字游戏中的 for 循环帮助
- 井字游戏的这个最小最大值实现有什么问题?
- C++井字游戏程序优化
- C++井字游戏不显示X字符
- 从森林里造一棵n元树
- C++:检查谁赢得了井字游戏
- 井字输入问题