繁体   English   中英

如何修复 C99 中的隐式声明错误?

[英]How to fix a implicit declaration error in C99?

我不明白为什么我会在 getline() 调用周围出现这个错误。 我已经导入了 stdlib.h,但我仍然收到“隐式声明”错误。

错误如下:

mopsolver.c: In function ‘take_input’:
mopsolver.c:54:35: error: passing argument 1 of ‘getline’ from incompatible pointer type [-Wincompatible-pointer-types]
     while ( 0 < (count = getline( &buf, &len, stdin)) ) {
                                   ^

mopsolver.c:21:5: note: expected ‘char *’ but argument is of type ‘char **’
 int getline(char line[], int maxline);
     ^~~~~~~
mopsolver.c:54:41: error: passing argument 2 of ‘getline’ makes integer from pointer without a cast [-Wint-conversion]
     while ( 0 < (count = getline( &buf, &len, stdin)) ) {
                                         ^

mopsolver.c:21:5: note: expected ‘int’ but argument is of type ‘size_t * {aka long unsigned int *}’
 int getline(char line[], int maxline);
     ^~~~~~~

mopsolver.c:54:26: error: too many arguments to function ‘getline’
     while ( 0 < (count = getline( &buf, &len, stdin)) ) {
                          ^~~~~~~
#include <getopt.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include "myQueue.h"

#define _DEFAULT_SOURCE
#define MAXQUEUE 256
int intArray[MAXQUEUE];
int itemCount = 0;

//This struct is created for each space in the map
typedef struct Space{
    char character;
    bool isVisited;
}Space;

/*
 * This function prints out the help menu to the standard input
 */
void help_menu(){
    printf("\n");
    printf("USAGE:\n");
    printf("mopsolver [-hdsp] [-i INFILE] [-o OUTFILE]\n");
    printf("\n");
    printf("Options:\n");
    printf("        -h      Print this helpful message to stdout and exit.\n");
    printf("        -d      Pretty print (display) the maze after reading.  (Default: off)\n");
    printf("        -s      Print shortest solution steps.        \t\t(Default: off)\n");
    printf("        -p      Print an optimal path.                \t\t(Default: off)\n");
    printf("        -i INFILE       Read maze from INFILE.        \t\t(Default: stdin)\n");
    printf("        -o OUTFILE      Write all output to OUTFILE.  \t\t(Default: stdout)\n\n");
}

int take_input(){
    char * buf = NULL;
    char *fileName=optarg;
    size_t len = 0;
    int count = 0;
    FILE * fp = fopen( fileName, "r");
    assert(fp);

    while ( 0 < (count = getline( &buf, &len, stdin)) ) {
        char * tok = strtok( buf, " \t\n");
        while ( tok ) {
            printf( "%c ", *tok);
            tok = strtok( NULL, " \n");
        }
        printf( "\n");
    }
    printf( "last count == %d\n", count);
    free( buf);
    fclose( fp);
    return EXIT_SUCCESS;
}

int main(int argc, char *argv []){
    //The options for the flags
    int opt;

    //checks to see if we were passed in the correct number of input
    if ( argc < 2 ) {
        fprintf( stderr, "usage: getfile filename\n");
        return EXIT_FAILURE;
    }

    //define {row,col}
    int N,M = 0;

    //checks the flags inputed
    while ((opt = getopt(argc  ,argv,"hdspi:o:")) != -1){
        switch (opt){
            case 'd':
                //pretty print
                take_input();
                break;
            case 'h':
                //help
                help_menu();
                break;
            case 's':
                //shortest solution

                break;
            case 'p':
                //optimal path

                break;
            case 'o':
                //output

                break;
            case 'i':
                //input
                take_input();
                break;
            default:break;
        }

    }

    return 0;
}

我尝试了很多不同的方法来解决这个问题,但似乎没有任何效果。 C noob在这里请帮助:/

请参阅在章节“功能测试宏要求”手册页

glibc 的功能测试宏要求(请参阅 feature_test_macros(7)):

   getline(), getdelim():
       Since glibc 2.10:
           _POSIX_C_SOURCE >= 200809L

这意味着你需要把

#define _POSIX_C_SOURCE 200809L

所有#include语句之前 (即源文件的开头)。 如果不这样做,头文件将不会声明getlinegetdelim

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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