在c++中开始一个nQueens游戏.哪里是错误位于我的代码

Beginning of an nQueens game in c++. Where is the error located in my code?

本文关键字:错误 于我的 代码 我的 nQueens 开始 c++ 一个 游戏      更新时间:2023-10-16

在尝试编译以下代码时,我一直得到错误。错误是

expected ',' or ';' before '{' token.

显示bool check_row(x)后面的括号有错误如果我把它注释掉,同样的情况也发生在bool check_col(x)上。如果我没有正确地定义函数,我就会回头看我的书,但它们在逻辑上似乎是正确的。这是一个在4x4棋盘上的nQueens游戏的开始。数字1代表女王。这两个布尔函数用于检查行和列是否空闲。startGame()将所有盒子赋值为0,showBoard()显示电路板的结果。

#include <cstdlib>
#include <iostream>
using namespace std;
int x=0, y=0;
int square[4][4];
void startGame()
{
    for(x=0;x<4;x++)
    {
        for(y=0;y<4;y++)
        {
            square[x][y]=0;
        }
    }
}
void showBoard()
{
    for(int x=0;x<4;x++)
    {
        if(x!=0)
        {
            cout<<endl;
        }
        for(int y=0;y<4;y++)
        {
            cout<<square[x][y];
        }
    }
    cout<<endl;
}
bool check_row(x)
{
    for(y=0;y<4;y++)
    {
        if(square[x][y]==1)
        {
            return false;
        }
        else if(square[x][y]==0)
        {
            if(y==3)
            {
                return true;
            }
            continue;
        }
    }
}
bool check_col(y)
{
    for(int x=0;x<4;x++)
    {
        if(square[x][y]==1)
        {
            return false;
        }
        else if(square[x][y]==0)
        {
            if(x==3)
            {
                return true;
            }
            continue;
        }
    }
}
int main(){
    startGame();
    showBoard();
return 0;
}

bool check_col(y)不是一个有效的原型。您需要为y(例如bool check_col(int y))提供一个类型。这同样适用于bool check_row(x)

您必须指定您在函数中传递的datatype作为parameter .考虑到xyint类型并且是local,您的function prototype应该是

bool check_row(int x)
bool check_col(int y)

如果xyglobal,那么不需要传递它们…

bool check_row()
bool check_col()

将工作,因为global variablesvisibility将在整个程序中,除非阴影