Expected before numeric constant

Got two errors when compiling my program.

1
2
3
4
5
6
7
8
9
#include "Token.h"
#include <vector>
using namespace std;

class Grid
{
	private:
		vector<vector<Token*> > grid(6,7);
};


1
2
/Users/steve/Desktop/Connect 4/Grid.h:8: error: expected identifier before numeric constant
/Users/steve/Desktop/Connect 4/Grid.h:8: error: expected ',' or '...' before numeric constant


Any suggestions would be greatly appreciated.
Last edited on
1) You cannot call ctors in class definitions like that

2) You cannot create a 2D vector in a single ctor call like that.


To do this you need to write a ctor for Grid, and put the sizing in there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Grid
{
public:
  Grid()  // default ctor
  {
    // set size of grid
    grid.resize(6);
    for(int i = 0; i < 6; ++i)
      grid[i].resize(7);
  }

private:
  vector< vector<Token*> > grid;
};
You can also do that in a single line using an initializer list
1
2
3
4
5
6
7
class Grid
{
    private:
        vector<vector<Token*> > grid;
    public:
       Grid() : grid ( 6, vector<Token*>(7) ) {}
};
Last edited on
Ah. yeah good call. For whatever reason I didn't think of that XD
1
2
3
4
5
6
7
{
    // set size of grid
    grid.resize(6);
    for(int i = 0; i < 6; ++i)
      grid[i].resize(7);
  }


Could this be placed in the Grid.cpp file?
Constructors are as all other member functions so you can move their bodies from the header to a source file
Topic archived. No new replies allowed.