a stupid question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C++ Shark

    a stupid question

    Hi, the following doesn't compile.

    #include<iostre am>
    #include<vector >
    using namespace std;
    class tile_type {
    vector<vector<i nt> > 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.
  • tom_usenet

    #2
    Re: a stupid question

    On 25 Jun 2003 09:46:13 -0700, cpp_shark@yahoo .com (C++ Shark) wrote:
    [color=blue]
    >Hi, the following doesn't compile.
    >
    >#include<iostr eam>
    >#include<vecto r>
    >using namespace std;
    >class tile_type {
    > vector<vector<i nt> > grid(3, vector<int> (3));
    > public:[/color]

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

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

    Tom

    Comment

    Working...