1

I am making a domineering game for my coursework, I have been trying to do something new by adding a lot of void functions and now for some weird reason my board isn't working as it is saying identifier "board" is undefined but I have never really had a problem with my board until now and I haven't figured it out yet. The error is in the void playAt() function.

#include <iostream>
#include <Windows.h>
#include <vector>
using namespace std;
int horizontal = 0;
int vertical = 0;
int row = 0;
int column = 0;
int x = row = 0;
int y = column = 0;
bool gameOver;
bool player = horizontal && vertical;
bool islegal;

void Setup()
{
    gameOver = false;
}
void Draw()
{
    // specifty default value to fill the vector elements
    int mapSize = 0;

    cout << "Enter the size of board => ";
    cin >> mapSize;

    vector<vector<char> > board(mapSize, vector<char>(mapSize, 'e'));
    for (int i = 0; i < mapSize; i++) {
        for (int j = 0; j < mapSize; j++) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}
void play()
{
    if (player == horizontal) {
        cout << ("Horizontal to play");
    }
    else {
        cout << ("Vertical to play");
    }
}
void playAt(int row, int column, bool player)
{
    board[row][column] = true;
    if (player == horizontal) {
        board[x][y + 1] = true;
        board[x][y + 2] = true;
    }
    else {
        board[x + 1][y] = true;
        board[x + 2][y] = true;
    }
}
void Input()
{
}
void Logic()
{
}
int main()
{
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
    }
}
  • uh what `voidAt()` function? is it missing or have you made a typo? – Object object Mar 12 '20 at 21:10
  • Does it say that `board` is not declared in that scope? If so, where is `board` declared? – Ted Lyngmo Mar 12 '20 at 21:11
  • 2
    `board` is local to `Draw`. It must either be passed to `playAt` as a parameter, or be a global variable, or something else. – HolyBlackCat Mar 12 '20 at 21:11
  • 2
    You are struggling with [scope](https://en.wikipedia.org/wiki/Scope_(computer_science)). `board` is defined and only exists within `Draw`. – user4581301 Mar 12 '20 at 21:11
  • I mean void playAt my bad –  Mar 12 '20 at 21:13
  • 1
    I'm absolutely flabbergasted that I can't find a good duplicate for this. My google-fu is weak today. – user4581301 Mar 12 '20 at 21:15
  • I think I now understand thank you guys. –  Mar 12 '20 at 21:20
  • Regarding _"I have never really had a problem with my board until now"_: you changed your code and broke it. This is the leading reason why previously-working code suddenly breaks. You should make only small changes and test often, until you have developed sufficient skill to interpret compiler errors, fix basic mistakes, and debug small programs. Keeping regular backups and using source control helps too. – paddy Mar 12 '20 at 21:21
  • Extending on Paddy's point, I don't like to write code until I know how I'm going to test it. That often suggests a good limit on the amount of code you want to write before compiling and testing. Plus if you work out how something going to be tested, you've also probably come up with a bunch of good ways to write it to pass that test. – user4581301 Mar 12 '20 at 21:23
  • Walk back your steps - what was the change you made when the problem started? That was the cause! If you can't determine what the change was, you're not using version control (or keeping backups) and should start doing that now. – Asteroids With Wings Mar 12 '20 at 21:26
  • 1
    Related [Are Global Variables Bad?](https://stackoverflow.com/questions/484635/are-global-variables-bad) – JohnFilleau Mar 12 '20 at 21:50

2 Answers2

1

When we declare a name, such as your board, we do so in a certain scope. Outside that scope, the name is inaccessible. Your vector<vector<char>> board name is local to function Draw() and its scope, since it is defined and declared only there. Outside that function's scope it is not visible to other portions of your code.

One quick (but debatable) solution to your problem is to declare the board name in a global scope, where it is visible to everyone, for example before the void Setup() function definition:

std::vector<std::vector<char>> board;

A better solution is to declare and initialize it inside the main() function and pass it as a parameter to other functions in which case you would need to change the function signatures.

That being said, the use of using namespace std; is best avoided.

Ron
  • 14,674
  • 4
  • 34
  • 47
0

Your issue is that board is indeed undefined within playAt(). The board variable is local to Draw(), outside the scope of Draw() it doesn't exist!

You need to either pass it (from a scope in which it exists like Draw()) to the functions you want to be able to use it or elevate its scope (i.e to the global level like with horizontal and everything else declared at the top). Generally prefer passing it or even better put all those functions inside an object with a class member board (it is C ++ after all).

Object object
  • 1,939
  • 10
  • 19