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;
}
3
  • possible duplicate of C++ Returning multidimension array from function Commented May 25, 2013 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. Commented May 25, 2013 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 Commented May 25, 2013 at 11:58

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

How do i declare a two dimensional array of Square objects, which actually calling constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.