我想知道是否有人可以借给我一些关于为什么我的代码不会编译的建议。我有一个简单的tic tac toe游戏,这里分成3个文件。然而,main.cpp无法从我的ticTacToe.h文件创建对象,我只是不明白为什么。我觉得我离这个跑步还有一步之遥。 ticTacToe.cpp和ticTacToe.h编译得很好。它是main.cpp文件,当它到达第14行" ttt.create_board();"时会产生错误。错误消息是"未识别的引用&t; ticTacToe :: create_board(char)'"任何帮助将不胜感激。谢谢你们。
main.cpp
#include "ticTacToe.h"
#include <iostream>
#include <limits> //This is required to catch invalid user input
void run_game()
{
char letter_o = 'O';
char letter_x = 'X';
ticTacToe ttt; //creates an object "ttt" to be used later
ttt.player1Piece = letter_x;
ttt.player2Piece = letter_o;
std::cout << "Welcome to tic tac toe!" << std::endl;
std::cout << "Here is a blank board: " << std::endl;
ttt.create_board();
while (true){
std::cout << "\nPlayer X, it is your turn\n" << std::endl;
ttt.update_board(letter_x);
if (ttt.determine_winner(letter_x) == true){
break;
}
std::cout << "\nPlayer O, it is your turn\n" << std::endl;
ttt.update_board(letter_o);
if (ttt.determine_winner(letter_o) == true){
break;
}
}
}
int main() //main kicks things off by calling "run_game()"
{
run_game();
}
ticTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
class ticTacToe
{
public: //Allow us to use the functions anywhere
char board[3][3]; //Creates an 2d array for the board
char player1Piece; //variable used in multiple functions
char player2Piece; //varibable used in multiple
void create_board();
void print_board(char playerPiece, int pos1 = 0, int pos2 = 0);
int check_for_overlap(int pos1, int pos2, char playerPiece);
void update_board(char playerPiece);
int determine_winner(char playerPiece);
};
#endif // TICTACTOE_H
ticTacToe.cpp
#include "ticTacToe.h"
#include <iostream>
#include <limits>
ticTacToe::ticTacToe()
{
void ticTacToe::create_board()
{
//initializes a blank board
for(int a = 0; a < 3; a++){
std::cout << "\n";
for (int b = 0; b < 3; b++){
board[a][b] = '-';
std::cout << board[a][b];
}
}
}
void ticTacToe::print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
{
//prints out the updated board when called upon to do so
for(int a = 0; a < 3; a++){
std::cout << "\n";
for (int b = 0; b < 3; b++){
board[pos1][pos2] = playerPiece;
std::cout << board[a][b];
}
}
}
int ticTacToe::check_for_overlap(int pos1, int pos2, char playerPiece)
{
if (board[pos1-1][pos2-1] != '-'){
std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
return true;
}
return false;
}
void ticTacToe::update_board(char playerPiece)
{
//updates the board based on user input
int x, y;
std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
while (true){
std::cout << "Enter row: " << std::endl;
std::cin >> x;
if (x < 1 || x > 3 || std::cin.fail()){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Your number is invalid. Try again. " << std::endl;
} else {
break;
}
}
while (true)
{
std::cout << "Enter col: " << std::endl;
std::cin >> y;
if (y < 1 || y > 3 || std::cin.fail()){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Your number is invalid. Try again. " << std::endl;
} else {
break;
}
}
if (check_for_overlap(x, y, player1Piece) == true){
x--;y--;print_board(player2Piece, x, y);
std::cout << "\nThe board has been re-set. Try again!" << std::endl;
} else if (check_for_overlap(x, y, player2Piece) == true){
x--;y--;print_board(player1Piece, x, y);
std::cout << "\nThe board has been re-set. Try again." << std::endl;
} else {
x--;y--;print_board(playerPiece, x, y);
}
}
int ticTacToe::determine_winner(char playerPiece)
{
/*slices the board and checks if playerPiece occupies that slot.
If the player makes a line, print that playerPiece has won
and exit the game.*/
if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
}
else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
return true;
} else {
return false;
}
}
}
}
答案 0 :(得分:2)
只需删除}
和
ticTacToe.cpp
即可
ticTacToe::ticTacToe()
{
您没有在头文件中声明任何构造函数。
然后函数print_board
还有另一个问题。
它被声明为void print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
,但你也在定义中使用默认参数。
将int pos1=0, int pos2=0
中的int pos1, int pos2
更改为ticTacToe.cpp
。
尝试过度思考代码格式,以了解哪个大括号属于哪个。 :)
答案 1 :(得分:1)
更改此
ticTacToe::ticTacToe()
{
void ticTacToe::create_board()
{
//initializes a blank board
for(int a = 0; a < 3; a++){
std::cout << "\n";
for (int b = 0; b < 3; b++){
board[a][b] = '-';
std::cout << board[a][b];
}
}
}
到这个
void ticTacToe::create_board()
{
//initializes a blank board
for(int a = 0; a < 3; a++){
std::cout << "\n";
for (int b = 0; b < 3; b++){
board[a][b] = '-';
std::cout << board[a][b];
}
}
}
然后从ticTacToe.cpp文件中删除文件中的最后两个大括号。
如果你想拥有一个构造函数,那么你应该在类中定义它,就像其他函数一样(然后当然在.cpp中实现它。)
class ticTacToe {
...
char player2Piece; //varibable used in multiple
ticTacToe(); <--- HERE
void create_board();
void print_board(char playerPiece, int pos1, int pos2); <-- Change the default arguments here
...
};
另外,在
void print_board(char playerPiece, int pos1, int pos2);
你不应该传递.h文件中的默认参数(或者相反)。
答案 2 :(得分:0)
问题出在这里
ticTacToe::ticTacToe()
{
void ticTacToe::create_board()
{
}
// more functions
}
您已将所有成员函数包装在tictacToe构造函数中。由于您实际上没有在构造函数中执行任何操作,因此可以将其删除。
删除
ticTacToe::ticTacToe()
{
和
}
}
在你的cpp文件的末尾。
同样不要为方法定义(cpp文件)和方法声明(h文件)中的方法指定默认参数。
如果你这样做
class ticTacToe
{
public: //Allow us to use the functions anywhere
...
void print_board(char playerPiece, int pos1 = 0, int pos2 = 0);
...
};
然后不要重新声明默认值。
void ticTacToe::print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
而是声明方法
void ticTacToe::print_board(char playerPiece, int pos1, int pos2)
最后评论
check_for_overlap
返回int
。当您在内部使用bool
并在调用时检查bool
,您应该将方法签名更改为
bool ticTacToe::check_for_overlap(int pos1, int pos2, char playerPiece)