如何解析"被叫对象不是函数或函数指针"错误?

时间:2016-09-10 05:23:58

标签: c pointers

我是C的新手,我正在尝试制作一个骰子游戏。我正在尝试学习指针,并且需要一些关于使用在main中初始化的调用函数和指针的提示和建议。

我在第一次点名功能上得到并错误,以确定第一个滚筒:

  

名为object" first_roll_call'不是函数或函数指针

这是什么意思,我该如何解决?

这是我的代码:

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

int main () {
    int dice;
    int roll, first_roll;
    int i;
    int first_roll_call;

    int *playerA;
    int *playerB;
    int *first_rollA;
    int *first_rollB;

    playerA = (int *)malloc(100);
    playerB = (int *)malloc(100);
    first_rollA = (int *)malloc(100);
    first_rollB = (int *)malloc(100);

    printf("Welcome to Dice Mania!\n\n");
    printf("First player to a combined score of 25 wins!\n");
    printf("We to see who goes first, press 1 to continue:");
    scanf("%d", &first_roll);

    for (i = 1; i < 2; i++){
        first_roll = rand() % 6 + 1;
        first_roll_call(first_roll, i, &first_rollA, &first_rollB);
    }
    if (*first_rollA > *first_rollB){
        printf("Player 1 goes first!\n");
    }
    else {
        printf("Player 2 goes first!\n");
    }

    if (dice == 1) {
        while (dice == 1) {
            printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
            srand(time(NULL));
            roll = rand() % 6 + 1;
            printf("You have rolled a: \n\n");
            dice = dice_roll(roll);
            if (dice == 0) {
                printf("\n\nThank you for playing");
                break;
            }
        }
    }

    return 0;
}

int dice_roll(int x) {
    int dice;

    if (x == 1) {
        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -------  *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 2) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*      |    *\n");
        printf("*  ____|    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 3) {
        int dice;

        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*   ___|    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -----    *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 4) {
        int dice;

        printf("*************\n");
        printf("*  |    |   *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*************\n*");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 5) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*       |   *\n");
        printf("*  _____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }
    if (x == 6) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        printf("Please press 1 to initiate the dice roll, 0 to exit\n\n");
        scanf("%d", &dice);
        return(dice);
    }

int first_roll_call(int x, int y, int *first_rollA, int *first_rollB){
    int dice;

    if (x == 1) {
        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -------  *\n");
        printf("*************\n");

        if (y = 1){
            printf("Player one rolled a %d", x);
            x = *first_rollA;

        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
    if (x == 2) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*      |    *\n");
        printf("*  ____|    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if (x == 3) {
        int dice;

        printf("*************\n");
        printf("*  -----    *\n");
        printf("*      |    *\n");
        printf("*   ___|    *\n");
        printf("*      |    *\n");
        printf("*      |    *\n");
        printf("*  -----    *\n");
        printf("*************\n");

        if (y = 1){
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if (x == 4) {
        int dice;

        printf("*************\n");
        printf("*  |    |   *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*       |   *\n");
        printf("*************\n*");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
    if (x == 5) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*       |   *\n");
        printf("*  _____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }

    if(x == 6) {
        int dice;

        printf("*************\n");
        printf("*  _____    *\n");
        printf("*  |        *\n");
        printf("*  |____    *\n");
        printf("*  |    |   *\n");
        printf("*  |____|   *\n");
        printf("*           *\n");
        printf("*************\n");

        if (y = 1) {
            printf("Player one rolled a %d", x);
            x = *first_rollA;
        }
        if (y = 2) {
            printf("Player two rolled a %d", x);
            x = *first_rollB;
        }
    }
}

}

2 个答案:

答案 0 :(得分:4)

编译器抱怨main()中的这一行:

        first_roll_call(first_roll, i, &first_rollA, &first_rollB);

它抱怨first_roll_call不是一个功能是完全正确的。在main()早期,您声明它是int类型的局部变量:

    int first_roll_call;

main()内,该声明会影响同名的任何其他对象。如果你想将它声明为函数返回 int,那么至少你需要这个,而不是:

    int first_roll_call();

但是,你应该为该函数声明一个完整的原型:

    int first_roll_call(int, int, int *, int *);

此外,虽然允许在其他函数中声明函数(但来嵌套函数体!),但是将函数声明放在文件范围内,靠近顶部的函数声明更为常见。源文件。

对于这样一个相当简单的情况,将函数main()移动到源文件的末尾将不再需要它来声明其他函数。编译器在到达main()时已经看到了附加到它们的函数体的声明,这些声明已经足够了。

答案 1 :(得分:1)

在C中,您必须先声明一个函数,然后才能使用它。编译器严格从上到下读取源代码。

因此,要么将整个first_roll_call函数移动到文件的顶部,要么在文件顶部写下以下行。

int first_roll_call(int x, int y, int *first_rollA, int *first_rollB);

此行声明了该函数,但保留了定义以供日后使用。