【问题标题】:warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration]警告:函数“colcheck”的隐式声明 [-Wimplicit-function-declaration]
【发布时间】:2021-01-10 09:13:02
【问题描述】:

我是 C 编程的新手,这就是我对它的语法感到困惑的原因。 问题:数独游戏使用 9 × 9 的网格,其中每一列和每一行,以及 九个 3 × 3 子网格中的每一个,都必须包含所有数字 1 ⋅ ⋅ ⋅ 9。图 4.26 给出了一个有效数独谜题的例子。该项目包括 设计一个多线程应用程序,确定解决方案是否 数独谜题是有效的。 有几种不同的方法可以对这个应用程序进行多线程处理。唯一的那个 建议的策略是创建检查以下标准的线程: • 检查每列是否包含数字 1 到 9 的线程 • 检查每一行是否包含数字 1 到 9 的线程

#include <stdlib.h>

int i,j,m,b,k;
void main()
{
    int a[9][9]={1,2,3,4,5,6,7,8,9,
                4,5,6,7,8,9,1,2,3,
                7,8,9,1,2,3,4,5,6,
                2,3,4,5,6,7,8,9,1,
                5,6,7,8,9,1,2,3,4,
                8,9,1,2,3,4,5,6,7,
                3,4,5,6,7,8,9,1,2,
                6,7,8,9,1,2,3,4,5,
                9,1,2,3,4,5,6,7,8};
                
                
    if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
    {
        printf("Success");
    }
    else{
        printf("Failed");
    }
}

int rowcheck(int a[9][9])
{
    int c[10]={0};
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            c[a[i][j]]++;
        }
        for(k=1;k<=9;k++)
            if(c[k]!=1)
            {
                printf("The value %d came %d times in %d row \n",k,c[k],i+1);
                return 0;
            }
            for(k=1;k<=9;k++)
                c[k]=0;
    }
    return 1;
}

int colcheck(int a[9][9])
{
    int c[10]={0};
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            c[a[i][j]]++;
        }
        for(k=1;k<=9;k++)
            if(c[k]!=1)
            {
                printf("The value %d came %d times in %d column \n",k,c[k],i+1);
                return 0;
            }
            for(k=1;k<=9;k++)
                c[k]=0;
    }
    return 1;
}

int cubecheck(int a[9][9])
{
    int c[10]={0},count=0;
    for(m=0;m<9;m+=3)
    {
        for(b=0;b<9;b+=3)
        {
            for(i=m;i<m+3;i++)
            {
                for(j=b;j<b+3;j++)
                {
                    c[a[i][j]]++;
                }
            }
            count++;
            for(k=1;k<=9;k++)
                if(c[k]!=1)
                {
                    printf("The value %d came %d times in %d box\n",k,c[k],count);
                    return 0;
                }
                for(k=1;k<=9;k++)
                    c[k]=0;
        }
    }
    return 1;
}```

I am getting this error plz help.

```proj1.c: In function ‘main’:
proj1.c:18:8: warning: implicit declaration of function ‘rowcheck’ [-Wimplicit-function-declaration]
     if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
        ^~~~~~~~
proj1.c:18:26: warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration]
     if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
                          ^~~~~~~~
proj1.c:18:43: warning: implicit declaration of function ‘cubecheck’ [-Wimplicit-function-declaration]
     if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
                                           ^~~~~~~~~
proj1.c: At top level:
proj1.c:136:1: error: expected identifier or ‘(’ before ‘}’ token
 }```
 ^


【问题讨论】:

  • 声明上面的所有函数main这样的函数int cubecheck(int a[9][9]);

标签: c syntax-error


【解决方案1】:

您需要在 main() 方法之前为您的函数声明,因为您在之后定义它们:。前向函数声明只是告诉编译器,在您的代码中的某处,将定义一个具有相同名称的函数,在这种情况下,在 main() 中使用它之后。

通常,在 C/C++ 中,您在头文件中定义函数原型,然后将它们包含在您的主源代码中,以便在调用函数后可以安全地定义函数。

#include <stdio.h>
#include <stdlib.h>

// add forward declarations for your functions here
int rowcheck(int [][9]);
int colcheck(int [][9]);
int cubecheck(int [][9]);

int main() {
    // your code
    return 0:
}

// method definitions afterwards

int rowcheck(int a[9][9]) {
    // your definition here
}

int colcheck(int a[9][9]) {
    // your definition here
}

int cubecheck(int a[9][9]) {
    // your definition here
}

另外,将main 方法定义为int main() 而不是void main()

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多