【问题标题】:too many arguments - function with int argument参数太多 - 带有 int 参数的函数
【发布时间】:2015-01-13 16:21:53
【问题描述】:

此代码是程序的一部分,它检查 3x3 矩阵的行。 矩阵的值为“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个数的错误?

【问题讨论】:

  • check_row的原型,对比函数的定义。
  • 我会读、说和写德语——但这是说英语的stackoverflow,所以请翻译你的变量名!

标签: c function arguments


【解决方案1】:
void check_row(void);

应该是

bool check_row (int row);

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

【讨论】:

    【解决方案2】:

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

    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 之前来省去一些麻烦,而不必担心需要单独声明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多