a stupid question

Discussion in 'C++' started by C++ Shark, Jun 25, 2003.

  1. C++ Shark

    C++ Shark Guest

    Hi, the following doesn't compile.

    #include<iostream>
    #include<vector>
    using namespace std;
    class tile_type {
    vector<vector<int> > grid(3, vector<int> (3));
    public:

    };
    int main()
    {
    tile_type k;

    return 0;
    }

    The compiler (g++) says:

    tile.cpp:5: invalid data member initialization
    tile.cpp:5: (use `=' to initialize static data members)
    make: *** [tile] Error 1

    I am trying to make an object with an array, so that I can access
    k.grid[][]. I am using Dietel & Dietel's book, and though I looked it
    over, I couldn't find a right way of doing this type of thing. I hope
    you can tell me the correct method.

    thanking you,
    Craig.
     
    C++ Shark, Jun 25, 2003
    #1
    1. Advertisements

  2. C++ Shark

    tom_usenet Guest

    You can only initialize non-static members in a constructor
    initializer list. So you want:

    vector<vector<int> > grid;
    public:
    tile_type(): grid(3, vector<int>(3))
    // comma delimit initializers for other members
    // and base classes
    {
    }

    Tom
     
    tom_usenet, Jun 25, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.