【问题标题】:I don't understand why compiler is giving me error with this code我不明白为什么编译器给我这段代码的错误
【发布时间】:2012-05-23 02:37:16
【问题描述】:

我有以下 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 );
}

编译器给出以下警告和错误消息。知道这里有什么问题吗?

./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 )

【问题讨论】:

    标签: c linux gcc clang x86-64


    【解决方案1】:

    删除 struct 关键字(不需要,因为您已经 typedefed 结构):

    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 ) 
    {
      ...
    

    欲了解更多信息,请参阅Why should we typedef a struct so often in C?

    【讨论】:

    • 更好的解决方案:不要使用 typedef!
    • William:但是在 ANSI C 中你必须在 struct 前面加上一个结构变量,对吧!在 C++ 中,我同意不需要 struct。不过不确定 C99。
    【解决方案2】:

    无论是你

    struct Timer
    {
        struct timeval td_start;
        struct timeval td_end;
    };
    
    void startTimer( struct Timer* ptimer ) 
    {
        gettimeofday( &(ptimer->td_start), NULL );
    }
    

    或者你

    typedef struct
    {
        struct timeval td_start;
        struct timeval td_end;
    } Timer;
    
    void startTimer( Timer* ptimer ) 
    {
        gettimeofday( &(ptimer->td_start), NULL );
    }
    

    但不要混淆。

    【讨论】:

      【解决方案3】:

      你创建了一个叫做Timer的类型,只是把函数参数前面的struct这个词删掉,像这样,例如:

      void startTimer( Timer* ptimer ) 
      {
          gettimeofday( &(ptimer->td_start), NULL );
      }
      

      【讨论】:

        【解决方案4】:

        错误的原因是,当你到达这里时

        void startTimer( struct Timer* ptimer ) 
        

        范围内没有struct Timer(只是匿名结构的typedef)。所以编译器认为你想要声明一个 new 类型 struct Timer 并使用指向它的指针作为参数。

        实际上这样做并没有多大用处,因为类型只是在函数内部可见。这使得从函数外部传入参数几乎是不可能的。

        所以编译器说,虽然语言可能允许,但这看起来不是一个好主意!

        【讨论】:

          猜你喜欢
          • 2023-02-01
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-28
          • 1970-01-01
          • 1970-01-01
          • 2014-04-11
          相关资源
          最近更新 更多