-1

main:

#include <iostream>
#include "common.h"
#include "squares.h"
#include "board.h"
using namespace std;
int Board::board_length=8;
int main()
{
    Board *tabla=new Board();
    tabla->printBoard();
}

board.h :

#ifndef BOARD_H
#define BOARD_H
#include "squares.h"

class Board
{
   static int board_length;
   Square boardSquares[board_length][board_length];
   public:
   Board();
   void printBoard();

};

error line 8 in board.h C:\c++ projects\CHESS-P3\board.h|8|error: array bound is not an integer constant before ']' token|

palalele
  • 21
  • 6

2 Answers2

2

As the error message indicates, board_length is not a constant value. To fix that, change the line

static int board_length;

in board.h to

static const int board_length = 8;

and remove the line

int Board::board_length=8;

from your main file. This should compile, but I cannot tell for sure, since you did not provide a minimal, reproducible example.

Bonus: To avoid a memory leak you should also change

Board *tabla=new Board();
tabla->printBoard();

in main just to

Board tabla;
tabla.printBoard();

Since you do not seem to be passing the Board instance around, there is no need to use a pointer here.

As a general rule of thumb: Whenever there is a new somewhere, there also needs to be a corresponding delete. Otherwise your pogram may leak memory. While this is no big deal for a small, short-running example program, it can become a serious issue for a program that runs for a long time.

Striezel
  • 3,693
  • 7
  • 23
  • 37
0

This is a nice use case for an int template:

board.h

template<int _board_length>
class Board
{
   static const int board_length = _board_length;
   int boardSquares[_board_length][_board_length];
   public:
   Board(){};
   void printBoard(){};

};

main.cpp

...
constexpr int BL=8;

//odr declare the static member if it matters
template<>
const int Board<BL>::board_length;


int main() {
    Board<BL> tabla;
    tabla.printBoard();
}

That way, the actual dimensions of the array are defined in the main and not in the include file where the class is declared, and all the methods in Board can know the actual size.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252