Visibility of globally defined Vectors in Subfunctions

Can anyone see the mistake?
I am generating a vector<vector<bool> > in an global namespace. Subfunctions of Main() are accessing this vector. Compiling makes no trouble, writing does not seem to make trouble, nor reading.
Problem: I am writing all fields with true but read afterwards all fields with false in different functions. When i am doing the same in the same function it seemes to write and read the values correctly, until reading with a different function.
How can this be? All subfunctions of main() should access the same global variable. Where is my mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>

using namespace std;

//defining global variables

namespace global
{
    int sizex=5;
    int sizey=5;
    int x=0;
    int y=0;
    vector<bool> first;
    vector<vector<bool> > pattern;
}

using namespace global;

//Functions

void write_vector_rand();
bool access_vector(int x, int y);
void write_vector(bool pixel, int x, int y);

int main()
{
    //creating empty vectors for later use.
    first.assign(sizex,0);
    pattern.assign(sizey,first);
    write_vector_rand();

}

//writing some data to all fields

void write_vector_rand()
{
    for(int i=0; i<sizey; i++)
    {
        for(int j=0; j<sizex; j++)
        {
            write_vector(/*Some Data e.g.*/ true,j,i);
            //Should be all true but is all false.
            cout<<access_vector(j,i)<<" ";
        }
        cout<<endl;
    }
    return;
};

//Accessing data

bool access_vector(int x, int y)
{
    first=pattern.at(y);
    return first.at(x);
}

//writing single data

void write_vector(bool pixel, int x, int y)
{
    first=pattern[y];
    first[x] = pixel;
    return;
}

write_vector modifies the first vector but the pattern vector is left unchanged.
Ok. How did i miss this mistake? It is obvious. Thanks for helping me.
Topic can be closed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Accessing data
bool access_vector(int x, int y)
{
    /// first=pattern.at(y);
    /// return first.at(x);
    return first.at(y);  ///
}

//writing single data

void write_vector(bool pixel, int x, int y)
{
    /// first=pattern[y];
    first[x] = pixel;
    return;
}
Topic archived. No new replies allowed.