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

时间:2018-10-31 20:53:49

标签: c 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在这里请帮助:/

1 个答案:

答案 0 :(得分:3)

请参见manpage中的“功能测试宏要求”部分:

  

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

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

这意味着您需要放置

#define _POSIX_C_SOURCE 200809L

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