-1

I'm getting the errors shown in the title. I'm sure it's just a simple mistake, but i haven't figured it out.The code is so simple because I just started with C++. I am trying to make a Tic tac toe game, but the error holds me back. Any advice is welcome! :)

main.cpp:

#include <iostream>
#include "Game.h"

using namespace std;

int main()
{
char board[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };

cout << "*** Tic tac toe ***" << endl;

Game game001(char board[3][3]);

game001.printBoard();
}

Game.h

#pragma once
class Game
{
public:
Game(char a [3][3]);

void printBoard();

private:
char _board[3][3];
};

Game.cpp:

#include<iostream>
#include "Game.h"

 using namespace std;

Game::Game(char a [3][3])
{
_board[3][3] = a[3][3];
}

void Game::printBoard()
{
cout << _board[0][0] << endl;
}
RioT1133
  • 11
  • 3

1 Answers1

1

There are several mistakes in your code.

Game game001(char board[3][3]); declares a function, not instantiates an object. The correct syntax is Game game001(board); instead.

_board[3][3] = a[3][3]; is wrong, too. It will compile, but it wont work at runtime. It copies only 1 char not the entire array, and arrays are 0 indexed so [3] is out of bounds. You need to loop through each dimension of the arrays copying each char.

Same for printBoard(), which outputs only 1 char instead of the entire array.

Try something more like this instead:

#pragma once
typedef char Board[3][3];
class Game {
public:
    Game(Board &a);
    void printBoard();
private:
    Board _board;
};

#include <iostream>
#include "Game.h"

using namespace std;

Game::Game(Board &a) {
    for(int row = 0; row < 3; ++row) {
        for(int col = 0; col < 3; ++col) {
            _board[row][col] = a[row][col];
        }
    }
} 

void Game::printBoard() {
    for(int row = 0; row < 3; ++row) {
        for(int col = 0; col < 3; ++col) {
            cout << _board[row][col] << ' ';
        }
        cout << endl;
    }
}

#include <iostream>
#include "Game.h"

using namespace std;

int main() {
    Board board = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
    cout << "*** Tic tac toe ***" << endl;
    Game game001(board);
    game001.printBoard();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So i need to change the char _board[3][3] and char and board[3][3] to Board board and Board _board and pass board by reference? – RioT1133 Nov 11 '17 at 17:39
  • @RioT1133 you don't *have* to, but it makes things a lot easier – Remy Lebeau Nov 11 '17 at 17:52
  • One question, what does typedef char Board[3][3]; do? And is passing by reference is essential? – RioT1133 Nov 11 '17 at 17:54
  • @RioT1133 the `typedef` creates an alias `Board` for the `char[3][3]` type, which you are using in multiple pieces of code. The typedef reduces the duplication, and makes the code easier to read and understand. As for the reference, it is not strictly needed, but it helps avoid making a redundant copy of the array data when calling the constructor. – Remy Lebeau Nov 11 '17 at 18:04
  • I understand now, thank you for your patience with me. – RioT1133 Nov 11 '17 at 18:09