在课堂上使用数组

时间:2015-03-21 17:41:59

标签: c++

我在使用数组时遇到问题 我的.h文件:

#ifndef SUDOKU_H
#define SUDOKU_H

class sudoku {
public:
    sudoku();
    bool findUnassignedLocation(int, int, int);
    bool isSafe(int, int, int);
    bool usedInRow(int, int, int);
    bool usedInCol(int, int, int);
    bool usedInBox(int, int, int, int);
    void printGrid(int);
    void solveSudoku(int);
private:
    static const int N;
    static const int UNASSIGNED;
};

#endif  /* SUDOKU_H */

我的.cpp文件:

#include "sudoku.h"

sudoku::sudoku() {
    N = 9;
    UNASSIGNED = 0;
}

bool sudoku::findUnassignedLocation(int grid[N][N], int &row, int &col){
    for (row = 0; row < N; row++)
        for (col = 0; col < N; col++)
            if (grid[row][col] == UNASSIGNED)
                return true;
        return false;
}

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

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

bool sudoku::usedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num){
    for (int row = 0; row < 3; row++)
        for (int col = 0; col < 3; col++)
            if (grid[row+boxStartRow][col+boxStartCol] == num)
                return true;
    return false;
}

bool sudoku::isSafe(int grid[N][N], int row, int col, int num){
    return !usedInBox(grid, row - row % 3 , col - col % 3, num) &&
           !usedInCol(grid, col, num) && !usedInRow(grid, row, num); 
}

void sudoku::solveSudoku(int grid[N][N]){
    int row, col;
    if(!findUnassignedLocation(grid, row, col)){
        printGrid(grid);
    }

    for (int num = 1; num <= 9; num++){
        if(isSafe(grid, row, col, num)){
            grid[row][col] = num;
            if(solveSudoku(grid))
                printGrid(grid);
            grid[row][col] = UNASSIGNED;
        }
    }
    std::cout << "No Solution" << std::endl;
}

void sudoku::printGrid(int grid[N][N]){
    for (int row = 0; row < N; row++){
        for (int col = 0; col < N; col++)
             std::cout<<grid[row][col]<<"  ";
        std::cout<<std::endl;
    }
}

我的主要档案:

#include <cstdlib>
#include <iostream>
#include "sudoku.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    sudoku S;
    int grid[9][9] = {{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}};

    S.solveSudoku(grid);
    return 0;
}

我不明白为什么我的输出会显示此错误:

main.cpp: In function 'int main(int, char**)':
main.cpp:28:23: error: invalid conversion from 'int (*)[9]' to 'int' [-fpermissive]
     S.solveSudoku(grid);
In file included from main.cpp:8:0:
sudoku.h:20:10: note: initializing argument 1 of 'void sudoku::solveSudoku(int)'
     void solveSudoku(int);

我认为问题在于指针,但我从C ++开始,所以我找不到解决方案。

由于

2 个答案:

答案 0 :(得分:0)

成员函数声明为

void solveSudoku(int);

它有一个int类型的参数。

您调用此功能

S.solveSudoku(grid);

传递给它一个二维数组

int grid[9][9]

编译器无法将指向类型为int ( * )[9]的数组的第一个元素转换为int类型的对象。

似乎存在拼写错误,因为在成员函数声明之后,您将其定义为

void sudoku::solveSudoku(int grid[N][N]){
//...

因此更新类定义中的函数声明。

也是这个构造函数

sudoku::sudoku() {
    N = 9;
    UNASSIGNED = 0;
}

无效,因为N和UNASSIGNED被声明为常量静态成员

static const int N;
static const int UNASSIGNED;

答案 1 :(得分:0)

是的,我明白了。但我不知道申报成员函数的正确方法