-
2-D Vectors
I am attempting to create a class that uses a vector of vectors to store a "grid" of integers. I am having some trouble getting it to work and I was hoping someone could help me out. My constructor should initialize the grid with 0s. This is compiling though I am not sure that it is working correctly. I wanted to test this by implementing an output method to display the grid contents. The program is compiling, though a call to the output method from main results in an error:
Request for member 'output' in 'initialGrid', which is of non-class type 'Grid()()'
call to output in main:
Code:
Grid exampleGrid();
exampleGrid.output(cout);
grid default constructor:
Code:
Grid::Grid()
{
for (int f=0; f<10; f++) {
Grid_.push_back(vector<int>());
for (int g=0; g<10; g++) {
Grid_[g].push_back(0);
}
}
}
output method:
Code:
void Grid::output(ostream &out) const
{
for (int f=0; f<10; f++) {
for (int g=0; g<10; g++) {
out << Grid_[f][g];
}
}
}
Thanks!
-
You have the right idea. The only problem here is a small logic error. Should you be using "g" as the subscript? Perhaps "f" would be the more logical choice.
-
The output is right, but your input is flawed as you are pushing back a value on to an index that may or may not have been created yet. Like hawaiian robots said, in the second portion of your code the index variable 'g' should be an 'f'.
-
thanks guys. i have fixed the logical error, though I am still receiving the error when using the output member function. any other ideas?
and this may be a stupid question, but do I have to implement the [ ] operator?