C Sudoku solver Setting values to multi dimensional arrays I
C++ Sudoku solver. Setting values to multi dimensional arrays.
In my .h file I have a function called mySudoku(int anArray[][Size]); I have an alternate constructor in Sudoku.cpp called mySudoku::mySudoku(int anArray[][Size]) (*Size = 9) and I have to set the values of a different array called Data[9][9] to the values passed into the constructor. I have tried many different ways and everytime I get the error: incompatable types int to int[9][9]. or I get the error: undefined reference to \'vtable for mySudoku\' I can\'t seem to figure out how to set int (*)[9] equal to a array [9][9]. Any help would be appreciated.
Solution
mySudoku.h:
#ifndef MYSUDOKU_H_
#define MYSUDOKU_H_
class mySudoku{
private:
int data[9][9];
static const int Size = 9;
public:
mySudoku(int anArray[][Size]);
void print();
};
#endif
____________________________________________________________________________________
mySudoku.cpp:
#include \"mySudoku.h\"
#include <iostream>
using namespace std;
mySudoku::mySudoku(int anArray[][Size]){
// data is a static reference to the array, so we can\'t set an array pointer directly to data
// data = anArray is incorrect
// correct way is to assign every element seperately
for (int i = 0; i < Size; ++i){
for (int j = 0; j < Size; ++j){
data[i][j] = anArray[i][j];
}
}
}
void mySudoku::print(){
for (int i = 0; i < Size; ++i){
for (int j = 0; j < Size; ++j){
cout << data[i][j] << \" \";
}
cout << endl;
}
cout << endl;
}
___________________________________________________________________________________________
sudokuTest.cpp:
#include <iostream>
#include \"mySudoku.h\"
using namespace std;
int main(){
int data[9][9] = { {1, 2, 3, 4, 5, 6, 7, 8, 9},
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
mySudoku sudoku(data);
sudoku.print();
return 0;
}
____________________________________________________________________________________________