Base Class Constructor for 2D Object Array

I am working on a sudoku solver that finds all solutions. To obtain multiple solutions I am using two objects. The first class is "Cell" to store the value, possible values, if it is solved, etc. The second class "Puzzle" is derived from class "Cell" and is a 2D array of "Cell" objects. I plan to copy the Puzzle object into a new Puzzle object and push one onto a stack to solve later for another solution.

I believe the error is in my Puzzle class constructor for private member board[9]9] (this is the 2D array of Cell objects). board is initialized in Puzzle.h as

Puzzle.h
1
2
3
4
5
6
7
8
9
#include "Cell.h"
class Puzzle : public Cell
{
 public: 
  Puzzle(void);

 private:
  Cell board[9][9];
};//end clas Puzzle 


Cell.h
1
2
3
4
5
class Cell 
{
  public:
	Cell(int row, int col, int input_value);
}//end Cell.h 


Puzzle.cpp
1
2
3
4
5
6
7
#include "Cell.h"
#include "Puzzle.h"
using namespace std;
Puzzle::Puzzle(void) : Cell(0,0,0)
{

}//end constructor 


**My member functions were left out for brevity.

I receive the following error messages.

1
2
3
4
5
6
7
8
Puzzle.cpp: In constructor ‘Puzzle::Puzzle()’:
Puzzle.cpp:13:34: error: no matching function for call to ‘Cell::Cell()’
Puzzle.cpp:13:34: note: candidates are:
In file included from Puzzle.cpp:7:0:
Cell.h:8:2: note: Cell::Cell(int, int, int)
Cell.h:8:2: note:   candidate expects 3 arguments, 0 provided
Cell.h:5:7: note: Cell::Cell(const Cell&)
Cell.h:5:7: note:   candidate expects 1 argument, 0 provided



It seems to me that I gave the Cell constructor 3 arguments with Puzzle::Puzzle(void) : Cell(0,0,0)

I believe and I could be wrong that the problem occurs when I try to create a puzzle object in my main() program by declaring

Puzzle puzzle_board;

and the 81 cell objects try to be created but are somehow not initialized.

My questions:
How do I initialize all 81 Cell objects in board?
Is there a way to have the default constructor called when board is created?

I can post the whole files if needed.

Thank you for helping me out! I have been scouring the internet for advice but to no avail.
Last edited on
Your constructor init list doesn't make sense. You have no member called "Cell" in your class.

EDIT: Actually, I just realized you are deriving Puzzle from Cell...what is your reasoning here?

If you just want the default constructor called for that array, you don't have to do anything, it will be called automatically for you.
Last edited on
First of all, a Puzzle can't be derived from a Cell (unless Puzzle is a Cell, in which case you're dealing with some kind of fractal sudoku!)

A C-style member array of Cells needs Cell to have a default constructor, unless you're up to typing (or metaprogramming) a C++11 initializer that lists 9x9 Cell(0,0,0)s, that is,
Puzzle::Puzzle(void) : board{{Cell(0,0,0), Cell(0,0,0), ...

If you don't have a suitable matrix class at hand, why not just use a vector?

1
2
3
4
5
6
7
8
9
class Puzzle
{
 public: 
  Puzzle(void) : board(9*9, Cell(0,0,0))
  { }

 private:
  vector<Cell> board;
};//end clas Puzzle  
Last edited on
Does it not need to be? I figured Puzzle would need to be derived from Cell to use functions like

board[i][j].getValue();

though now thinking about it I may not need it to be derived just need the header included. Thanks for the check on that.

However, the same error occurs when I make Puzzle not derived any longer.
I have also tried excluding the Cell constructor

1
2
3
4
Puzzle::Puzzle(void) : Cell(0,0,0)
{

}//end constructor 


to

1
2
3
4
Puzzle::Puzzle(void) 
{

}//end constructor 


but that didn't work either.
If it helps. I had board hold pointers to the Cell objects and it compiled fine. I can't use that though because I need to eventually copy the objects into an array again to store the instance.
When you create the array of Cells the default ctor is called, but since you overload the ctor the default is not automatically provided and you need to. See these simplified examples:
http://ideone.com/PSnon6
http://ideone.com/2a7Gnk
Topic archived. No new replies allowed.