5

I'm trying to do something like:

#include <iostream>
#include <vector>
#include <ctime>

class Clickomania
{
    public:
        Clickomania();
        std::vector<std::vector<int> > board;
};

Clickomania::Clickomania()
    : board(12, std::vector<int>(8,0))             <<<<<<<
{

    srand((unsigned)time(0));

    for(int i = 0; i < 12; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            int color = (rand() % 6) + 1;
            board[i][j] = color;
        }
    }
}

However, apparently I can't initialize the "board" vector of vectors this way.

How can I create a public member of a 2d vector type and initialize it properly?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
FrankTheTank
  • 107
  • 1
  • 2
  • 5
  • 3
    Why can you not initialize it this way? (aside from the fact that you should probably call `srand` elsewhere, rather than each time you create a board). – James McNellis Apr 28 '10 at 23:33
  • 1
    It compiles fine in Visual Studio 2005. What's the compiler error, exactly? – In silico Apr 28 '10 at 23:34
  • Compiles and looks fine. Still, you should call the `srand` somewhere else and provide parameters instead of "magic numbers" `12, 8, 0, 6, etc... – M. Williams Apr 28 '10 at 23:37
  • Can you elaborate on any errors you're getting? The above looks fine. Stylistically, I'd prefer you preincrement `i` and `j` instead of postincrement, but that won't impact the result. – Nathan Ernst Apr 28 '10 at 23:43
  • 1
    I'm using VS2010 and tried to create a 2d vector using the above code. I get a compiler error under the '[j]' in "board[i][j] = color;" - "expression must have pointer-to-object type". I'd almost make a question of this myself if this one wasn't so closely related. – T. Webster Sep 17 '10 at 12:21
  • 1
    @Inquisitor What did you define `board` as? – Jonathan Mee Feb 03 '15 at 21:11

3 Answers3

12

you should use the constructor that allows you to specify size and initial value for both vectors which may make it a bit easier altogether.

something like:

vector<vector<int>> v2DVector(3, vector<int>(2,0));

should work.

sanimalp
  • 799
  • 5
  • 12
5

Use a matrix instead:

(Basic example from boost documentation)

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
  • 3
    Maybe because it had nothing to do with getting vector to work in the above code like the question asked. I don't know. I think that downvoting it is a bit harsh - this answer *is* potentially helpful after all - but it doesn't technically answer the question. – Jonathan M Davis Apr 29 '10 at 00:07
  • 3
    +1, I don't find there is any need to down vote a helpful answer even if it is not exactly answering the OP's question. – AndersK Apr 29 '10 at 01:18
3

Compiling your code with g++, the error I get is that neither srand() nor rand() were declared. I had to add #include <cstdlib> for the code to compile. But once I did that, it worked just fine. So, I'd say that other than adding that include statement, your code is fine. You're initializing the vector correctly.

Perhaps the code you have doesn't quite match what you posted? I would assume that if your actual code didn't include cstdlib, that you would have quickly understood that that was the problem rather than something with vector. So, if your code doesn't quite match what you posted, maybe that's the problem. If not, what compiler are you using?

Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102