[英]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.