Linux 我不明白为什么编译器给我这个代码错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10603182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:20:41  来源:igfitidea点击:

I don't understand why compiler is giving me error with this code

clinuxgccclangx86-64

提问by pythonic

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.

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

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 )

采纳答案by NPE

Remove the structkeyword (it's not needed since you've already typedefed the struct):

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

void startTimer( Timer* ptimer ) 
{
  ...

void stopTimer( Timer* ptimer ) 
{
  ...

Alternatively, remove the typedef:

或者,删除typedef

struct Timer
{
    struct timeval td_start;
    struct timeval td_end;
};

void startTimer( struct Timer* ptimer ) 
{
  ...

void stopTimer( struct Timer* ptimer ) 
{
  ...

For more information, see Why should we typedef a struct so often in C?

有关更多信息,请参阅为什么我们应该在 C 中如此频繁地对结构进行 typedef?

回答by Didier Trosset

Either you

要么你

struct Timer
{
    struct timeval td_start;
    struct timeval td_end;
};

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

Or you

或者您

typedef struct
{
    struct timeval td_start;
    struct timeval td_end;
} Timer;

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

But don't mixup.

但不要混淆。

回答by DGomez

You created a type called Timer, just delete the word struct before the function parameters, like this, for example:

你创建了一个叫 Timer 的类型,把函数参数前面的 struct 字去掉就行了,像这样,例如:

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

回答by Bo Persson

The reasonfor the error is that, when you get here

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

void startTimer( struct Timer* ptimer ) 

there is no struct Timerin scope (just a typedef for an anonymous struct). So the compiler believes you want to declare a newtype struct Timerand use a pointer to that as a parameter.

没有struct Timer范围(只是匿名结构的 typedef)。因此,编译器认为您要声明一个类型struct Timer并使用指向该类型的指针作为参数。

Actually doing that would be less than useful, as the type would just be visible insidethe function. That would make it practically impossible to pass in a parameter from outsidethe function.

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

So the compiler says that, while possibly allowed by the language, this doesn't look like a good idea!

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