简体   繁体   中英

Implicit declaration of function error… but did it like they said?

I copied code from a class example so I could play with it. So far I can't even get it to run as is.

Here it is:

#include <cs50.h>

int x = GetInt(); 
switch(x)
{
    case 1:
    printf("One!\n)")
    break;
    case 2:
    printf("Two!\n")
    break;
    case 3:
    printf("Three!\n")
    break;
    default:
    printf("Nah...\n")
}

The error is: sswb.c:3:9: error: implicit declaration of function 'GetInt' is invalid in C99 [-Werror,-Wimplicit-function-declaration] int x = GetInt();

The help thingy says "You seem to have an error in sswb.c on line 3. By "implicit declaration of function 'GetInt'", clang means that it doesn't recognize GetInt. Did you forget to #include <cs50.h> (in which GetInt is declared) atop your file?"

But I have so.... I'm lost.

Help? Please tell me what's right under my nose and I'm not seeing?

here is something simpler

#include <stdio.h>

int main() 
{
    int x;
    scanf("%d",&x);
    switch(x)
    {
        case 1: printf("one!\n");
        break;
        case 2: printf("two!\n");
        break;
        case 3: printf("three!\n");
        break;
        default: printf("error!\n");
        break;
    }
}

It seems the error occurred because you forgot to specify a string as an argument of the function. Something

int x = GetInt( "Enter an integer: " );

Or use

int x = get_int( "Enter an integer: " );

if GetInt does not exist.

Pay attention to that you may not place statements as the switch statement or declarations that are not initialized by constant expressions in a file scope. They shall be inside functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM