0

Getting error message saying: SudokuGame\sudoku_solver.h:6: error: multiple definition of `grid'

Can anyone point out why? I guess i'm including sudoku_solver.h in the wrong way

See parts of my code files below.

sudoku_solver.cpp:

#include <iostream>
#include "sudoku_solver.h"

using namespace std;

bool isPresentInCol(int col, int num) {
        for (int row = 0; row < N; row++)
                if (grid[row][col] == num)
                        return true;
        return false;
}

sudoku_solver.h:

#ifndef SUDOKU_SOLVER_H
#define SUDOKU_SOLVER_H

#define N 9

int grid[N][N] = {
   {3, 0, 6, 5, 0, 8, 4, 0, 0},
   {5, 2, 0, 0, 0, 0, 0, 0, 0},
   {0, 8, 7, 0, 0, 0, 0, 3, 1},
   {0, 0, 3, 0, 1, 0, 0, 8, 0},
   {9, 0, 0, 8, 6, 3, 0, 0, 5},
   {0, 5, 0, 0, 9, 0, 6, 0, 0},
   {1, 3, 0, 0, 0, 0, 2, 5, 0},
   {0, 0, 0, 0, 0, 0, 0, 7, 4},
   {0, 0, 5, 2, 0, 6, 3, 0, 0}
};

/*
*Check if number is present in given coloum
*/
bool isPresentInCol(int col, int num);

/*
*Check if number is present in given row
*/
bool isPresentInRow(int row, int num);

#endif // SUDOKU_SOLVER_H

main.cpp:

#include "sudoku_solver.h"
#include <iostream>
using namespace std;

int main()
{
    if (solveSudoku())
        printSolvedSudoku();
    else
        cout << "No solution exists";

    return 0;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
razz
  • 47
  • 5
  • 2
    If you include suduko_solver.h in more that one cpp file you will get more than one definition of the global variable grid which defined in that header. You should define grid in the cpp file and declare grid in the header. – Anon Mail May 02 '21 at 17:02
  • Does this answer your question? https://stackoverflow.com/questions/1945846/what-should-go-into-an-h-file Or this? https://stackoverflow.com/questions/38942013/declaring-variables-in-header-files-c/38942057 – Drew Dormann May 02 '21 at 17:10

1 Answers1

0

sudoku_solver.h is included in both sudoku_solver.cpp and main.cpp. Hence two definitions of the global variable grid.

wydadman
  • 36
  • 5