invalid use of undefined type ‘struct Square’?

59 Views Asked by At

I am making a program that solves a sudoku game, but I received an error from my compiler.

error: invalid use of undefined type ‘struct Square’
   30 |         if(box->squares[x]->possible[number-1] == 0){
                                  ^~

the function is:

int updateBoxes(Square *** sudoku, int row, int column){
    int x;
    int number = sudoku[row][column]->number;
    Box * box;
    box = sudoku[row][column]->box;

    for(x = 0; x < SIZE_ROWS; x++){
        if(box->squares[x]->possible[number-1] == 0){
            box->squares[x]->solvable--;
            box->squares[x]->possible[number-1] = 1;
        }
    }
    return 1;
}

the error also aplies to the next two lines.

the definitions of Square and Box are:

typedef struct box
{
    struct Square ** squares;  /* array of Squares */
    int numbers;
    int possible[9];
    int solvable;
    struct Box * next;
} Box;

/* square is a single number in the board */
typedef struct square
{
    int number;     
    int possible[9];
    /*
       the array is only made of 0 and 1
       each index corresponds to a number: ex: index 4 == number 5 ...
       1 tells me that 5 cannot go into this scare
       0 tells me that 5 can go into this scare
    */
    int solvable;   /* will be subtracted 1 each time and when it is 1 it's because is solvable */
    Box * box;      /* tells me the corresponding box */
    int row;
    int column;
} Square;

I am following a tutorial on youtube and the guy doesn't get the error that I get, the youtube video is: https://www.youtube.com/watch?v=CnzrCBLCDhc&t=1326s

1

There are 1 best solutions below

3
Ted Lyngmo On BEST ANSWER

You've used struct Square where you should have used struct square in the Box definition. Corrected:

typedef struct box {
    struct square** squares; /* array of Squares */
//         ^
    int numbers;
    int possible[9];
    int solvable;
    struct box* next; // box, not Box
} Box;

Another option is to typedef Square before Box and use Square in Box:

typedef struct square Square;
typedef struct box Box;
struct box {
    Square** squares; /* array of Squares */
    int numbers;
    int possible[9];
    int solvable;
    Box* next;
};

Note that there is nothing called struct Square after these definitions. There is struct square and Square.