Global variable not declared in this scope

Hello all,

I'm trying the exercise Dungeon Crawl.
I made an apart function for the drawing of the board.
I made an array for it, a global variable.
In main I gave them all the char '.'
But when I want to use char board[7][9] for example in a function, it says:

board was not declared in this scope
From your explanation it appears your board is not global and is declared inside main(). Could you show your code please?
Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include "TekenBord.h"

using namespace std;

char bord[7][9];

int main()
{



    for (int b = 0 ;b != 8 ; b++ )
    {
    for (int a = 0 ;a != 10 ;a++ )
    bord[b][a] = '.';
    }

    TekenBord();


}


TekenBord.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

void TekenBord()
{

    cout<<bord[1][1]<<bord[1][2]<<bord[1][3]<<bord[1][4]<<bord[1][5]<<bord[1][6]<<bord[1][7]<<bord[1][8]<<bord[1][9]<<"\n";
    cout<<bord[2][1]<<bord[2][2]<<bord[2][3]<<bord[2][4]<<bord[2][5]<<bord[2][6]<<bord[2][7]<<bord[2][8]<<bord[2][9]<<"\n";
    cout<<bord[3][1]<<bord[3][2]<<bord[3][3]<<bord[3][4]<<bord[3][5]<<bord[3][6]<<bord[3][7]<<bord[3][8]<<bord[3][9]<<"\n";
    cout<<bord[4][1]<<bord[4][2]<<bord[4][3]<<bord[4][4]<<bord[4][5]<<bord[4][6]<<bord[4][7]<<bord[4][8]<<bord[4][9]<<"\n";
    cout<<bord[5][1]<<bord[5][2]<<bord[5][3]<<bord[5][4]<<bord[5][5]<<bord[5][6]<<bord[5][7]<<bord[5][8]<<bord[5][9]<<"\n";
    cout<<bord[6][1]<<bord[6][2]<<bord[6][3]<<bord[6][4]<<bord[6][5]<<bord[6][6]<<bord[6][7]<<bord[6][8]<<bord[6][9]<<"\n";
    cout<<bord[7][1]<<bord[7][2]<<bord[7][3]<<bord[7][4]<<bord[7][5]<<bord[7][6]<<bord[7][7]<<bord[7][8]<<bord[7][9]<<"\n";
}


TekenBord.h

1
2
void TekenBord();
Last edited on
Ah. You can't access globals defined in one header in another header like that. In TekenBord.h you need to add extern char bord[7][9];

Btw, bord should be spelled board.
I know, but not in my language :p

and thanks for you help!
Last edited on
Now I get the error: Expected initialiser before 'using'

help :(
You have a typo in your header probably.
Ok, I found it, but now I still have bord was not declared :(
Also you need to #include "TekenBord.h" in tekenbord.cpp


Your array indexing in wrong in both main and tekenbord functions
Uhm, what exactly are you trying to say?
I don't understand, that indexing :s
If you have an array like this:

int myarray[5], then they are 5 elements, these are myarray[0] to myarray[4]

the [number_in_brackets] we use to access the array is called the index.

So as you can see, the index starts at 0 and goes up to size-1.
So this is wrong.
1
2
3
4
5
6
7
8
9
10
11
void TekenBord()
{

    cout<<bord[1][1]<<bord[1][2]<<bord[1][3]<<bord[1][4]<<bord[1][5]<<bord[1][6]<<bord[1][7]<<bord[1][8]<<bord[1][9]<<"\n";
    cout<<bord[2][1]<<bord[2][2]<<bord[2][3]<<bord[2][4]<<bord[2][5]<<bord[2][6]<<bord[2][7]<<bord[2][8]<<bord[2][9]<<"\n";
    cout<<bord[3][1]<<bord[3][2]<<bord[3][3]<<bord[3][4]<<bord[3][5]<<bord[3][6]<<bord[3][7]<<bord[3][8]<<bord[3][9]<<"\n";
    cout<<bord[4][1]<<bord[4][2]<<bord[4][3]<<bord[4][4]<<bord[4][5]<<bord[4][6]<<bord[4][7]<<bord[4][8]<<bord[4][9]<<"\n";
    cout<<bord[5][1]<<bord[5][2]<<bord[5][3]<<bord[5][4]<<bord[5][5]<<bord[5][6]<<bord[5][7]<<bord[5][8]<<bord[5][9]<<"\n";
    cout<<bord[6][1]<<bord[6][2]<<bord[6][3]<<bord[6][4]<<bord[6][5]<<bord[6][6]<<bord[6][7]<<bord[6][8]<<bord[6][9]<<"\n";
    cout<<bord[7][1]<<bord[7][2]<<bord[7][3]<<bord[7][4]<<bord[7][5]<<bord[7][6]<<bord[7][7]<<bord[7][8]<<bord[7][9]<<"\n";
}

because the bord is declared as [7][9] in size, which means your index goes from [0][0] to [6][8]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{



    for (int b = 0 ;b != 8 ; b++ )//error - should be for (int b = 0 ;b < 7 ; b++ )
    {
    for (int a = 0 ;a != 10 ;a++ )//error - should be for (int a = 0 ;a <9 ;a++ )
    bord[b][a] = '.';
    }

    TekenBord();


}
Last edited on
Topic archived. No new replies allowed.