Tôi có mã C sau, có vẻ rất chính xác với tôi. Tuy nhiên, trình biên dịch clang (infact gcc hoặc bất kỳ trình biên dịch C nào khác) cũng nghĩ khác đi.Tôi không hiểu tại sao trình biên dịch lại cho tôi lỗi với mã này
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);
}
Trình biên dịch cung cấp thông báo lỗi sau đây &. Bất kỳ ý tưởng gì là sai ở đây?
./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)
Một giải pháp tốt hơn: không sử dụng typedef! –
William: Nhưng bạn phải tiền tố cấu trúc với một biến cấu trúc trong ANSI C đúng! Trong C++ tôi đồng ý struct là không cần thiết. Bạn không chắc chắn về C99. – pythonic