0

Can you please point me to the correct syntax. Here is the code so far

enum Color {WHITE, BLACK};

struct Square
{
  Square(Color p_color): color_(p_color) {}
  Color color_;
};

//instead of Square *, is there a clear way to express intention that function returns 
//Square[][]
Square[][] initSquare(const int rows, const int cols)
{
    Square board[rows][cols]; //Why does compiler complain that Square does not 
                              //have a default constructor? I am just declaring an 
                              //array of type Square

    for(int row=0;row<rows;row++)
            for(int col=0;col<cols;col++)
            {
                    if(col%2 == 0)
                            board[row][col]= Square(WHITE);
                    else
                            board[row][col] = Square(BLACK);
            }
      return board;
}
Jimm
  • 8,165
  • 16
  • 69
  • 118
  • possible duplicate of [C++ Returning multidimension array from function](http://stackoverflow.com/questions/3716595/c-returning-multidimension-array-from-function) – Mats Petersson May 25 '13 at 11:38
  • In C++ you can't have arrays with dynamic size, they have to have a predefined size. To declare them dynamically, use pointers and the "new" operator. – J-Mik May 25 '13 at 11:39
  • @MatsPetersson I am unclear how to declare a two dimensional array of objects, without invoking the constructor such as Square board[rows][cols]; The other link does not seem to help with this problem – Jimm May 25 '13 at 11:58

1 Answers1

2
Square board[rows][cols]; //Why does compiler complain that Square does not 
                          //have a default constructor? I am just declaring an 
                          //array of type Square

This invokes default constructor (i.e., Square::Square()). You have a constructor taking an argument. Compiler does not provide the default constructor if user overloaded the constructor. So compiler is complaining about it.

Secondly, you can not return the board from the function. board is a block scoped variable and it's life time ends as soon as the function returns. You should go for dynamic allocation instead.

Edit: Avoid dynamic allocations, if possible. Using std::vector can simplify the task much better. Google about STL container std::vector if you are not aware of it.

#include <vector>

using namespace std; 

enum Color {WHITE, BLACK};

struct Square
{
  Color color_;
};

typedef vector<vector<Square> > chessBoard;

chessBoard initSquare(int rows, int cols)
{
    chessBoard board;

    for (int i=0; i<rows; ++i)
    {
        vector<Square> vSqr(cols); // You can pass the argument to the constructor
                                   // giving the second parameter here. But I
                                   // changed your interface a bit.

        for (int j=0; j<cols; ++j)
        {
            vSqr[j].color_ = (j%2 == 0) ? WHITE : BLACK;
        }
        board.push_back(vSqr);
    }

    return board;
}

int main()
{
    chessBoard board = initSquare(8,8);
    return 0;
}
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • How do i declare a two dimensional array of Square objects, which actually calling constructor? – Jimm May 25 '13 at 11:48