我不明白为什么编译器会给我这个代码错误 [英] I don't understand why compiler is giving me error with this code
本文介绍了我不明白为什么编译器会给我这个代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下C代码,对我来说似乎很正确.但是,clang编译器(实际上是gcc或任何其他C编译器)也有不同的看法.
I have the following C code, which looks very correct to me. However, the clang compiler (infact gcc or any other C compiler too) thinks otherwise.
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 );
}
编译器给出以下警告和提示.错误消息.知道这里有什么问题吗?
The compiler gives the following waring & error messages. Any idea what is wrong here?
./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 )
推荐答案
删除struct
关键字(因为您已经typedef
删除了该结构,所以不需要它):
Remove the struct
keyword (it's not needed since you've already typedef
ed the struct):
void startTimer( Timer* ptimer )
{
...
void stopTimer( Timer* ptimer )
{
...
或者,删除typedef
:
struct Timer
{
struct timeval td_start;
struct timeval td_end;
};
void startTimer( struct Timer* ptimer )
{
...
void stopTimer( struct Timer* ptimer )
{
...
有关更多信息,请参见为什么我们应该键入def在C中经常使用的结构吗?
For more information, see Why should we typedef a struct so often in C?
这篇关于我不明白为什么编译器会给我这个代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文