0
class Suduko
{
   public:

int N=4;
int grid[N][]={{1,0,3,0}
               ,{0,0,2,1}
               ,{0,1,0,2}
               ,{2,4,0,0}};
bool solve()
{
    int i,j;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            if(grid[i][j]==0)
            break;
    }
    if(i==N && j==N)
        return true;
    for(int n=1;n<=N;n++)
    {
        if(issafe(i,j,n))
        {
            grid[i][j]=n;
            if(solve())
                return true;
            grid[i][j]=0;
        }
    }
    return false;
}
bool issafe(int i,int j,int n)
{
    for(int k=0;k<N;k++)
        if(grid[k][j]==n || grid[i][k]==n)
        return false;
    int s=sqrt(N);
    int rs=i-i%s;
    int cs=j-j%s;
    for(int i=0;i<s;i++)
        for(int j=0;j<s;j++)
        if(grid[i+rs][j+cs]==n)
        return false;
    return true;
}
void print()
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cout<<grid[i][j]<<" ";
    }
    cout<<endl;
}
};

When I try to solve the sudoku problem I got an error in line number 8 " Invalid use of non-static data member 'Suduko::N' ". I am new to C++, I don't figure out what is the cause of this error please help me to remove this error.

  • 1
    [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). All array sizes must be compile-time constants. – Some programmer dude Apr 14 '22 at 06:36
  • 4
    replace `int N=4;` (a variable initialized to the value of 4) with `static const int N=4;` (a constant unique for the class, set to a fixed value of 4) – codeling Apr 14 '22 at 06:39

0 Answers0