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?
#include <iostream>
#include <vector>
usingnamespace 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;
}
usingnamespace 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;
}