繁体   English   中英

如何解决类型冲突?

[英]How can I fix the typeconflict?

我编写了这个程序并收到以下错误消息:

sub.h:1:6: warning conflicting types for built-in function 'isdigit' bool isdigit(char c)

In file included from sub.c:1:0:
sub.h:1:1: error: unknown type name 'bool'
 bool isdigit(char c)

sub.h:1:6: warning: conflicting types for built-in function 'isdigit'
 bool isdigit(char c)

sub.c:7:6: error: conflicting types for 'isdigit'
 bool isdigit(char c)

In file included from sub.c:1:0:
sub.h:1:6: note: previous declaration of 'isdigit' was here
 bool isdigit(char c)
//main.c
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>
#include "sub.h"

#define N 120

int main (void){

    char eingabe[N+1]={0};
    int zahlen[10]={0};

    fgets(eingabe,N,stdin);

    for(int i=0; i<N; i++){
        if(isdigit(eingabe[i]))
        {
            int zahl = toNumber(eingabe[i]);
            zahlen[zahl]= zahlen[zahl]+1;
        }
    }

    for (int j=0; j<10; j++)
    {
        printf("%d: %d \n",j, zahlen[j]);
    }
    return 0;
}
// sub.h
bool isdigit(char c);
int toNumber(char c);
//sub.c
#include "sub.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

bool isdigit(char c){
    bool Antwort = false;
    for(int i='0'; i<= '9'; i++){
        if(c==i){
            Antwort = true;
        }
    }
    return Antwort;
}

int toNumber (char c){
    int num =0;
    num = c- '0';
    return num;
}

由于错误消息 state,您的 function isdigit与内置的同名 function 冲突。

这意味着您需要将此 function 重命名为不与内置 function 冲突的其他名称。

第一个错误告诉你已经有一个isdigit function,它是C标准库的内置function。 您必须更改标识符(名称)。 还要在您的sub.h文件中包含stdbool.h 否则,在包含sub.h之前,在所有 C 文件中包含stdbool.h 这是为了修复未知类型的bool错误,通过在sub.h之前包含stdbool.h定义类型并且您可以使用它。

暂无
暂无

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

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