Thread: 2-D Vectors

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Arrow 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!
    Last edited by newyorkcity; 05-02-2010 at 05:08 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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'.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2
    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?
    Last edited by newyorkcity; 05-02-2010 at 08:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using/ Searching through Multiple Vectors
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 08-03-2009, 08:35 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

Tags for this Thread