C++ 在编译我的第一个c+时遇到问题+;程序

C++ 在编译我的第一个c+时遇到问题+;程序,c++,C++,这是我的密码: #include<iostream> private int chessBoard[8][8]; void printBoard(int board[][]) { for(int i=0; i<8; i++) { for(int j=0; j<8; j++) { if(board[i][j]) { cout<<"Q

这是我的密码:

#include<iostream>

private int chessBoard[8][8];

void printBoard(int board[][])
{
    for(int i=0; i<8; i++)
    {
        for(int j=0; j<8; j++)
        {
            if(board[i][j])
            {
                cout<<"Q ";
            }

            else
            {
                cout<<"* ";
            }
        }
    cout<<endl;
    }
}

//This method will check if any queens are in attacking position to the left of the test queen placement
bool checkSpot(int board[][],int row, int col)
{
    bool safe = true;
    //checks the current row for queens
    for(int i=0; i<col, i++)
    {   
        if(board[row][i])
        safe=false;
    {

    //checks the upper diag
    for( int i=row, int j=col; i>0 && j>0; i--, j--)
    {
        if(board[i][j])
        safe=false;
    }

    //checks lower diag
    for(int i = row, int j=col; i<8 && j>0; i--, j++)
    {
        if(board[i][j])
        safe=false;
    }

    if(safe)
    {
        return true;
    }
    else
        return false;
}

bool solve(int board[][], int testCol)
{
    bool solved = false;
    bool safe;
    if(testCol==8)
    {
        solved = true;
        return solved;
    }

    for(int i =0; i>8; i++)
    {
        // test if the tested column(testCol) and the row(i) are both safe for the queen to be placed at then we can move into placing said queen and more onto the next column for
        // more solutions in this same method recursivly
        safe = checkSpot(board, i, testCol);
        if(safe)
        {
            //place the queen
            board[i][col]=1;
            //recursion to go back through this method in the next column
            if(solve(board[][], testCol+1)
            {
                solved = true;
                printBoard(board)
                return solved;

            }

            else
            {
                //if the queen cannot be placed, we have to remove the previous queens and move them until a solution is found.
                board[i][testCol]=0;

        }
    }

}       

int main()
{


    solve(chessBoard, 0);
}
其逻辑相当简单(至少我希望它能正常工作),但我甚至无法通过编译器。我能在这个问题上得到一些指导吗


你们都帮了我很大的忙,但遗憾的是我发现我只能用1d数组来解决这个问题,所以我不得不从头开始。再次感谢大家的帮助,你的建议将来一定会对我有所帮助

嗯,您的代码中有很多错误。让我们看看

首先,将
private

private int chessBoard[8][8];
您只需要对类的成员使用
private
。所以把它改成

int chessBoard[8][8];
void printBoard(int board[][8])
                          ^
                          it's okay to leave this one
接下来,在所有函数中,都有

void printBoard(int board[][])
                            ^
                            here, it's wrong
您需要提供大小,您只能跳过第一个大小,所有其他大小都必须提供,以便更好地将其更改为

int chessBoard[8][8];
void printBoard(int board[][8])
                          ^
                          it's okay to leave this one
对您的所有功能进行更改

您的代码的某些地方也缺少一些
}

差点忘了,您需要添加

using namespace std;
在标题后面,或者使用

std::cin
std::cout

而不是
cin
cout
endl

你也有

for(int i=0; i<col, i++)
                  ^
                  you need a semicolon here
把它改成

for( int i=row, j=col; i>0 && j>0; i--, j--)

这是一份汇编。至于我做了什么,有很多,每件事都要花一点时间写下来。需要注意的一个主要问题是循环的
。
你有:

for( int i=row, int j=col; i>0 && j>0; i--, j--)
{
    if(board[i][j])
      safe=false;
}
要对循环执行嵌套的
时,请执行以下操作:

for (int i = row;  i > 0; i--)
{
     for (int j = col; j > 0; j--)
     {
          if (board[i][j])
             safe = false;
     }
}
另外,请注意括号
{}()
,因为它们不一致,并且缺少一些

对于阵列,使用二维阵列时必须至少指定一个维度

你有:
int board[][]
在不同的地方。您必须有:
int board[][8]

另外,如果要使用
cout
endl
cin
等。。。您必须具有
包含命名空间std否则您将不得不使用:
std::cout
std::endl
std::cin
。 但不管怎样,现在应该可以编译了

#include<iostream>

using namespace std;

int chessBoard[8][8];

void printBoard(int board[][8])
{
    for (int i = 0; i<8; i++)
    {
        for (int j = 0; j<8; j++)
        {
            if (board[i][j])
            {
                cout << "Q ";
            }
            else
            {
                cout << "* ";
            }
        }
        cout << endl;
    }
}

//This method will check if any queens are in attacking position to the left of the test queen placement
bool checkSpot(int board[][8], int row, int col)
{
    bool safe = true;
    //checks the current row for queens
    for (int i = 0; i < col; i++)
    {
        if (board[row][i])
            safe = false;
        {
            //checks the upper diag
            for (int i = row;  i > 0; i--)
            {
                for (int j = col; j > 0; j--)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            //checks lower diag
            for (int i = row; i < 8;  i--)
            {
                for (int j = col; j > 0; j++)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            if (safe)
            {
                return true;
            }
            else
                return false;
        }
    }
}

        bool solve(int board[][8], int testCol)
        {
            bool solved = false;
            bool safe;
            if (testCol == 8)
            {
                solved = true;
                return solved;
            }

            for (int i = 0; i > 8; i++)
            {
                // test if the tested column(testCol) and the row(i) are both safe for the queen to be placed at then we can move into placing said queen and more onto the next column for
                // more solutions in this same method recursivly
                safe = checkSpot(board, i, testCol);
                if (safe)
                {
                    //place the queen
                    board[i][testCol] = 1;
                    //recursion to go back through this method in the next column
                    if (solve(board, testCol + 1))
                    {
                        solved = true;
                        printBoard(board);
                        return solved;
                    }
                    else
                    {
                        //if the queen cannot be placed, we have to remove the previous queens and move them until a solution is found.
                        board[i][testCol] = 0;
                    }
                }
            }
        }

            int main()
            {
                solve(chessBoard, 0);
            }
#包括
使用名称空间std;
国际棋盘[8][8];
无效打印板(int板[][8])
{

对于(int i=0;我从
private int chessBoard[8][8]中删除
private
。函数调用
solve(board[]],testCol+1)
是错误的,因为
[]
。删除它们。此外,您还缺少一个结尾
到同一行上的
if
语句。但是,还有更多的问题。例如
for(int i=0;i>8;i++)之类的循环显然是错误的。这不是造成编译问题的原因,但应该把它改成<代码> i < 8 <代码>。从一开始就帮我一个忙,学习C++。代替数组,使用向量。我希望我能使用向量,我更了解它们,但是我有一个老的时尚老师,说向量是为了简单起见,我们需要学习数组是如何工作的。如果我理解OP想要做什么,他正在检查对角线上是否有其他皇后。当你沿着对角线移动时,你必须同时增加/减少行和列。相反,你建议扫描所有的方块。这不是OP想要的,so对不起,恐怕这个答案是错的。你是对的。我的错。我没有看到那里的评论:/
#include<iostream>

using namespace std;

int chessBoard[8][8];

void printBoard(int board[][8])
{
    for (int i = 0; i<8; i++)
    {
        for (int j = 0; j<8; j++)
        {
            if (board[i][j])
            {
                cout << "Q ";
            }
            else
            {
                cout << "* ";
            }
        }
        cout << endl;
    }
}

//This method will check if any queens are in attacking position to the left of the test queen placement
bool checkSpot(int board[][8], int row, int col)
{
    bool safe = true;
    //checks the current row for queens
    for (int i = 0; i < col; i++)
    {
        if (board[row][i])
            safe = false;
        {
            //checks the upper diag
            for (int i = row;  i > 0; i--)
            {
                for (int j = col; j > 0; j--)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            //checks lower diag
            for (int i = row; i < 8;  i--)
            {
                for (int j = col; j > 0; j++)
                {
                    if (board[i][j])
                        safe = false;
                }
            }

            if (safe)
            {
                return true;
            }
            else
                return false;
        }
    }
}

        bool solve(int board[][8], int testCol)
        {
            bool solved = false;
            bool safe;
            if (testCol == 8)
            {
                solved = true;
                return solved;
            }

            for (int i = 0; i > 8; i++)
            {
                // test if the tested column(testCol) and the row(i) are both safe for the queen to be placed at then we can move into placing said queen and more onto the next column for
                // more solutions in this same method recursivly
                safe = checkSpot(board, i, testCol);
                if (safe)
                {
                    //place the queen
                    board[i][testCol] = 1;
                    //recursion to go back through this method in the next column
                    if (solve(board, testCol + 1))
                    {
                        solved = true;
                        printBoard(board);
                        return solved;
                    }
                    else
                    {
                        //if the queen cannot be placed, we have to remove the previous queens and move them until a solution is found.
                        board[i][testCol] = 0;
                    }
                }
            }
        }

            int main()
            {
                solve(chessBoard, 0);
            }