2011-02-24 43 views
0
#define TRIP 6 
#include <stdio.h> 

char error_area(char area_code, char S, char M, char L, char N, char P, char K, char R, char C, char U, char W, char O, char T, char F); 

int main(void) 
{ 
    char area_code, S, M, L, N, P, K, R, C, U, W, O, T, F, checkB, travelarea[TRIP]; 

    printf("Please select from the following that best describes your destination:\n"); /*area code input*/ 
    printf("S Small city - population under 50,000\n");         /*Choices for area code*/ 
    printf("M Medium city - population between 50,000 and 500,000\n"); 
    printf("L Large city - pop. over 500,000\n"); 
    printf("N Natural formation like a mountain, a lake, a cave, a geyser, a fjord, a canyon, etc.\n"); 
    printf("P Designated park or reserve such as a wildlife refuge, a national park, a bioreserve, or a protected marine area\n"); 
    printf("K Man made landmark like the Great Wall of China, the Taj Mahal, or Stonehenge\n"); 
    printf("R State or province or region of a country\n"); 
    printf("C Whole country\n"); 
    printf("U Multiple countries like traveling through Europe\n"); 
    printf("W Ocean voyage\n"); 
    printf("O Any other type of destination - such as visiting the sites of the seven wonders of the world\n"); 
    printf("Please enter the Area Letter code:"); 
    scanf("%c", &area_code); 

    checkB = error_area(area_code, S, M, L, N, P, K, R, C, U, W, O, T, F); 
    while (checkB == F)        /*error loop for error area code*/ 
    { 
     printf("Please re-enter a valid area_code:"); 
     scanf("%c", &area_code); 
     checkB = error_area(area_code, S, M, L, N, P, K, R, C, U, W, O, T, F); 
     if (checkB == T) 
    {travelarea[0]=area_code;} 
    } 

}  

error_area(area_code, S, M, L, N, P, K, R, C, U, W, O, T, F) /*area code error check*/ 
{ 

    if ((area_code == S) || (area_code == M) || (area_code == L) ||(area_code == N) ||(area_code == P) ||(area_code == K) || (area_code == R) ||(area_code == C) || (area_code == U) || (area_code == W) || (area_code == O)) 
    { 
     return T; 
    } 
    else 
    { 
     printf("Area code is invalid. (Please make sure code is capitalize)\n"); 

     return F ; 
    } 
} 

我得到这个错误:当我编译冲突类型的错误先前的声明___在这里

test2.c:40: error: conflicting types for âerror_areaâ 
test2.c:5: error: previous declaration of âerror_areaâ was here 

我不断收到这些错误信息,我敢肯定,我宣布在开始原型及其类型所以我不确定为什么会出现类型冲突。 我的一位导师告诉我,这是因为它被声明并且在底部定义了所有类型,我称之为error_area,但似乎不起作用。

回答

1

只是因为你声明的原型error_area功能并不意味着你现在可以自由在定义省略返回类型和参数类型。当你定义error_area你仍然应该指定所有类型的明确

char error_area(char area_code, char S, char M, char L, char N, char P, char K, char R, char C, char U, char W, char O, char T, char F) 
{ 
    ... 

相反,你没有明确的类型名称定义你的error_area。该定义是根据“旧”的规则解释,假定即所有失踪类型为int,所以你定义了什么是相当于

int error_area(int area_code, int S, ... /* and so on */ 
{ 
    ... 

这是你在原型说完全不同。所以编译器告诉你,你的声明与你的定义相矛盾。

+0

所以这意味着除了int以外的其他类型的函数我还需要在我调用函数时定义类型 – 2011-02-24 17:57:36

+0

@Thao Nguyen:“Call”?这与任何“呼叫”无关。你*声明*函数(原型)和你*定义*函数(实体)。在这两种情况下,所有类型都必须明确指定。而且,'int'也必须明确指定。只有过时的C语言版本才允许省略“int”。不再。 – AnT 2011-02-24 18:02:48

+0

@AndreyT:只要人们仍然重视向后兼容性,你会在实践中看到这些过时的版本。有时候,糟糕的决定,或者至少令人遗憾的决定,会持续很长时间。 – 2011-02-24 18:08:20