user062495
user062495

Reputation: 65

Printing a vector of vectors

I am trying to make a vector of vectors of type int and then print it out. I have this code in main.cpp.

Grid myGrid;
for (int i  = 0; i < myGrid.getdivas(); i++)
{
    for (int j = 0; j < myGrid.getdivas(); j++)
    {
        cout << myGrid[i][j];
    }
}

My constructor for a grid is

Grid::Grid()
{
    for (int i = 0; i < 10; i++) 
    {
        vector<int> row; 
        for (int j = 0; j < 10; j++) 
        {
            row.push_back(0); 
        }
        grid_.push_back(row);
    }
}

I keep getting an error in the cout statement:

'Grid' does not define this operator or a conversion to a type acceptable to the predefined operator

Upvotes: 1

Views: 164

Answers (1)

jrok
jrok

Reputation: 55395

That should be:

cout << myGrid.grid_[i][j]; // if grid_ is public

But you'll probably want to overload operator[] for you class. Better yet, have a member function that does the printing or overload operator<<.

Upvotes: 2

Related Questions