太多参数 - 使用int参数的函数

时间:2015-01-13 16:21:54

标签: c function arguments

此代码是程序的一部分,用于检查3x3 Matrix的行。 矩阵的值为'matrix [row] [col]',N为'3',之前已定义。

编译代码类似于:

error_handler.c:20:3: error: too many arguments to function ‘check_row’

实际代码如下:

#include <stdio.h>
#include "globals.h"

void check_row(void); //Prototype

bool check  (void)
{
int ergebnis_row, ergebnis_col, ergebnis_block;
ergebnis_row = FALSE;
ergebnis_col = FALSE;
ergebnis_block = FALSE;

int row, col;
for (row = 1; row <= N*N; row++)
{
     if (check_row(row) == TRUE)
    {
         ergebnis_row = TRUE;
    }
}

if ((ergebnis_row && ergebnis_col && ergebnis_block) == FALSE)
    return FALSE;
else
    return TRUE;
}



bool check_row (int row)
{
    int tester[9], col, i, truefalse;
truefalse = 1;

for (col = 1; col <=(N*N); col++)
{
    tester[col] = 0; //Initialisierung von tester und temp
}            //


for (col = 1; col <=(N*N); col++)
{
    tester[matrix[row][col]] += 1; //For each number, tester array is increased by 1

}

for (col = 1; col <=(N*N); col++)
{

    if (tester[col] > 1)
    {   
        truefalse--; // If there is an error, truefalse changes
        printf("\nZu viele %d an Reihe %d", (matrix[row][col]), row);
    }

}

if (truefalse == 1)
    return FALSE;
else
    return TRUE;

}

即使函数check_row中只有1个Argument'row',为什么编译器会给出有关Arguments数量的错误?

2 个答案:

答案 0 :(得分:2)

void check_row(void);

应该是

bool check_row (int row);

您的声明与定义更改您的函数声明并不匹配。原型应该改变

答案 1 :(得分:1)

您的函数声明与您的函数调用定义不匹配:

void check_row(void); // function declaration, specifies no arguments and no return value
...
if (check_row(row) == TRUE) // function call, passes 1 argument and uses return value
                            // conflicts with declaration
...
bool check_row (int row) // function definition, specifies bool return type and 1 int argument
                         // conflicts with declaration

您可以通过在check_row之前设置main定义来节省一些麻烦,而不用担心需要单独声明。