C++:错误,变量的多重定义
C++: error, multiple definition of variable
收到错误消息说:SudokuGame\sudoku_solver.h:6:错误:`grid'的多重定义
谁能指出原因?我想我以错误的方式包含 sudoku_solver.h
请参阅下面我的部分代码文件。
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;
}
sudoku_solver.h 包含在 sudoku_solver.cpp 和 main.cpp 中。因此全局变量网格的两个定义。
收到错误消息说:SudokuGame\sudoku_solver.h:6:错误:`grid'的多重定义
谁能指出原因?我想我以错误的方式包含 sudoku_solver.h
请参阅下面我的部分代码文件。
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;
}
sudoku_solver.h 包含在 sudoku_solver.cpp 和 main.cpp 中。因此全局变量网格的两个定义。