【问题标题】:Function not returning char correctly: error during compile函数未正确返回 char:编译期间出错
【发布时间】:2013-09-10 17:23:09
【问题描述】:

我目前正在尝试用 C 编写一个简单的 Rock, Paper, Scissors 程序。作业的重点是熟悉如何使用字符。这是我目前拥有的代码。它不完整,因为我被卡住了。

#include <stdio.h>

int main()
{
    int pScore = 0;
    int cScore = 0;
    int ties = 0;
    char play, go;
    char comp = 'r';

    printf("Welcome to Rock-Paper-Scissors!\n");
    printScore(pScore, cScore, ties);
    for ( ; ;)
    {
        printf("Do you want to play Rock-Paper-Scissors? (y/n): ");
        scanf("\n%c", &go);
        if(go == 'y' || go == 'Y')
        {
            printf("Enter your choice (r,p,s): ");
            scanf("\n%c", &play);
            comp = computerPlay();
            printf("Computer plays: %c", comp);
        }
        else
            break;
    }
    printf("\nThanks for Playing!!\n\n");
}

char computerPlay()
{
    int r = rand() % 3;
    char c;
    if (r == 0)
    {
        c = 'r';
    }
    else if (r == 1)
    {
        c = 'p';
    }
    else if (r == 2)
    {
        c = 's';
    }
    return c;
}

int printScore(int p, int c, int t)
{
    printf("\nHuman Wins: %d       Computer Wins: %d       Ties: %d\n\n", p, c, t);
    return 0;
}

编译器给我以下错误:

RPS.c:35:6: error: conflicting types for ‘computerPlay’

RPS.c:28:14: note: previous implicit declaration of ‘computerPlay’ was here

在我看来,这应该可以正常工作....我不知所措。

【问题讨论】:

  • 嘿。 char computerPlay() { return "rpc"[rand()%3]; } 对不起。无法抗拒。

标签: c function computer-science systems-programming chars


【解决方案1】:

你没有声明函数computerPlay()

声明此函数后检查。

char computerPlay(void);   

#include&lt;stdio.h&gt;之后添加这条语句

编辑:

C 中的所有标识符都需要在使用前声明。对于函数和变量都是如此。 对于函数,声明需要在函数的第一次调用之前。 完整的声明包括返回类型以及参数的数量和类型

简单示例:

int sum (int, int);    // declared a function with the name sum 

result =sum(10,20);    // function call  

int sum (int a, int b) // defined a function called sum 
    {
        return a + b;
    }

【讨论】:

  • 如果您不想导出函数,请将其设为静态...(等到您发现编译器的警告选项 :-))
  • @WhozCraig ,rockdadoot 是的,声明应该在 main 之前。声明应该是全局的(静态的或非静态的)并且在函数定义之前。如果他想限制范围,像这样的静态声明 static char computerPlay(void); 是个好主意。谢谢。
【解决方案2】:

编译器在遇到函数的使用时,需要知道函数的存在。

在您的代码中,当编译器(读取您的源文件)遇到comp = computerPlay(); 时,它没有看到任何告诉它该函数存在的信息。它猜测函数是什么,并猜测 int computerPlay(void) - 默认情况下,C 将始终假定函数返回 int。然后它稍后会遇到实际定义,这与它已经做出的猜测相冲突 - 函数返回char,而不是int

解决办法是让编译器在你使用它之前就知道函数的存在。您可以:

  • 将整个函数定义移到main上方
  • main上方声明static char computerPlay(void);,表明它仅适用于该源文件
  • 将声明 char computerPlay(void); 放入单独的头文件中,并将 #include 该文件放在 main 上方,从而使该函数可用于其他源文件(鉴于此问题范围之外的其他步骤)

【讨论】:

    【解决方案3】:

    你正在使用两个函数一个是

    char computerPlay() 第二个是

    int printScore(int p, int c, int t) 这两个都必须首先声明在包含语句的下方。

    因此,只需在包含语句下方添加以下语句即可。

    char computerPlay();

    int printScore(int , int , int );

    第二件事你也在使用 int main() 所以最后你必须返回整数所以你还必须在 main return 0 或任何其他整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2010-12-08
      • 2012-01-08
      相关资源
      最近更新 更多