2014-04-21 126 views
0

編譯器是給我的錯誤信息上靜態聲明如下非靜態聲明的錯誤消息

line 20: "static declaration of ‘timeDifference’ follows non-static declaration" 

然後又是一個在

line 12: "previous declaration of ‘timeDifference’ was here" 

我知道它是與我的功能'時間差異'。這裏是我的代碼:

#include <stdio.h> 

struct time 
{ 
    int hours; 
    int minutes; 
    int seconds; 
}; 

main() 
{ 
    int timeDifference (struct time diff); 

    struct time early, late, difference; 

    printf ("Enter Start Time hh:mm:ss "); 

    scanf ("%d:%d:%d", &early.hours, &early.minutes, &early.seconds); 

    printf ("Enter End Time hh:mm:ss "); 

    scanf ("%d:%d:%d", &late.hours, &late.minutes, &late.seconds); 

    int timeDifference (struct time diff) 
    { 
      if (late.seconds < early.seconds) 
      late.seconds += 60; 
      late.minutes -= 1; 
      if (late.minutes < early.minutes) 
      late.minutes += 60; 
      late.hours -= 1; 
      if (late.hours < early.hours) 
      late.hours += 24; 

      diff.seconds = late.seconds - early.seconds; 
      diff.minutes = late.minutes - early.minutes; 
      diff.hours = late.hours - early.hours; 
    } 

    return 0; 
} 
+1

你有函數的原型和定義在'main'的主體中。將原型移動到'main'之前,並將'main'之後的函數移動。 – AntonH

+1

@AntonH原型可以保持在主體內。 – bubble

+0

在嘗試之前先閱讀基本知識。 http://www.tutorialspoint.com/cprogramming/c_functions.htm –

回答

1

你不能讓裏面另外一個功能C.的timeDifference宣言第12行,並開始在第20行本身的功能(定義),需要在室外移動你的main()功能。

0

timeDifference的定義移到main()函數的外部。根據C標準,這樣的構造沒有被定義。有些編譯器會採用它,但在你的情況下,編譯器似乎已經放棄了。

0

不能聲明功能的其他功能裏面,移動爲TimeDifference的main 外面在你身邊沒有給main返回值類型,使其int main()

0

雖然下面的代碼是不完整的,也許它將沿着你的方式幫助你:

#include <stdio.h> 

struct time 
    { 
    int hours; 
    int minutes; 
    int seconds; 
    }; 

int timeDifference(
     struct time *I__early, 
     struct time *I__late, 
     struct time *_O_diff 
    ) 
    { 
    if(I__late->seconds < I__early->seconds) 
     I__late->seconds += 60; 

    I__late->minutes -= 1; 
    if(I__late->minutes < I__early->minutes) 
     I__late->minutes += 60; 

    I__late->hours -= 1; 
    if (I__late->hours < I__early->hours) 
     I__late->hours += 24; 

    _O_diff->seconds = I__late->seconds - I__early->seconds; 
    _O_diff->minutes = I__late->minutes - I__early->minutes; 
    _O_diff->hours = I__late->hours - I__early->hours; 

    return(0); 
    } 

int main() 
    { 
    struct time early, late, difference; 

    printf ("Enter Start Time hh:mm:ss "); 
    scanf ("%d:%d:%d", &early.hours, &early.minutes, &early.seconds); 
    printf ("Enter End Time hh:mm:ss "); 
    scanf ("%d:%d:%d", &late.hours, &late.minutes, &late.seconds); 

    timeDifference(&early, &late, &difference); 
    ... 

    return(0); 
    }