我正在尝试用C语言编写这个简单的代码,它要求用户给出一个正数,检查它是否是正数,然后只返回正数。
我得到了这个错误:
positive.c:28:7: warning: implicit declaration of function 'GetInt' is invalid
in C99 [-Wimplicit-function-declaration]
n = GetInt();
我会认为这意味着我没有声明我的一个函数,或者我没有调用某个库。据我所知,这一切都是我做的。下面是我的代码:
#include <stdio.h>
int GetPositiveInt(void);
int main(void)
{
int n = GetPositiveInt();
printf("Thanks for the %i\n", n);
}
/*This all gets called into the above bit*/
int GetPositiveInt(void)
{
int n; /*declare the variable*/
do
{
printf("Give me a positive integer: ");
n = GetInt();
}
while (n <= 0);
return n; /*return variable to above*/
}
有人知道为什么这会给我一个错误吗?
发布于 2013-10-10 14:18:16
这是因为这个函数GetInt
不存在,或者您忘记包含正确的头文件。
您可以将对函数GetInt
的调用替换为:
scanf("%d", &n);
发布于 2013-10-10 14:20:29
您还没有声明GetInt()
。编译器不知道它返回什么和接收什么参数。隐式声明在C99中是被禁止的(以前是有效的-只有当你启用C89时才会产生警告)。
当然,如果你只有声明而没有实现--你会在链接阶段得到一个错误。
发布于 2014-07-24 04:16:54
有一个为CS50 edX类创建的库,您需要将其包含在.c文件的顶部。否则,编译器不知道GetInt()函数。
#include <cs50.h>
https://stackoverflow.com/questions/19288370
复制相似问题