a stupid question

C

C++ Shark

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.
 
T

tom_usenet

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:

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top