我有以下C代碼,這對我來說看起來非常正確。但是,clang編譯器(實際上gcc或任何其他C編譯器)都認爲不然。我不明白爲什麼編譯器給我錯誤代碼
typedef struct
{
struct timeval td_start;
struct timeval td_end;
} Timer;
void startTimer(struct Timer* ptimer)
{
gettimeofday(&(ptimer->td_start), NULL);
}
void stopTimer(struct Timer* ptimer)
{
gettimeofday(&(ptimer->td_end), NULL);
}
該編譯器給出了以下waring &錯誤消息。任何想法這裏有什麼錯誤?
./timing.h:14:25: warning: declaration of 'struct Timer' will not be visible
outside of this function [-Wvisibility]
void startTimer(struct Timer* ptimer)
^
./timing.h:16:27: error: incomplete definition of type 'struct Timer'
gettimeofday(&(ptimer->td_start), NULL);
~~~~~~^
./timing.h:14:25: note: forward declaration of 'struct Timer'
void startTimer(struct Timer* ptimer)
^
./timing.h:19:24: warning: declaration of 'struct Timer' will not be visible
outside of this function [-Wvisibility]
void stopTimer(struct Timer* ptimer)
^
./timing.h:21:27: error: incomplete definition of type 'struct Timer'
gettimeofday(&(ptimer->td_end), NULL);
~~~~~~^
./timing.h:19:24: note: forward declaration of 'struct Timer'
void stopTimer(struct Timer* ptimer)
更好的解決方案:不要使用typedef! –
William:但是你必須在ANSI C的右邊結構變量的前綴結構!在C++中,我同意結構是不需要的。不知道C99。 – pythonic