C typedef struct不确定性

Ema*_* Ey 1 c struct typedef

考虑C中的以下typedef结构:

21:typedef struct source{
22: double  ds;             //ray step
23: double  rx,zx;          //source coords
24: double  rbox1, rbox2;   //the box that limits the range of the rays
25: double  freqx;          //source frequency
26:    int64_t  nThetas;        //number of launching angles
27:    double   theta1, thetaN; //first and last launching angle
28:}source_t;
Run Code Online (Sandbox Code Playgroud)

我得到错误:
globals.h:21:错误:重新定义'struct
source'globals.h:28:错误:'source_t'globals.h的冲突类型
:28:注意:'source_t'的先前声明在这里

我尝试过使用其他格式来定义:


struct source{
...
};
typedef struct source source_t;
Run Code Online (Sandbox Code Playgroud)


typedef struct{
...
}source_t;
Run Code Online (Sandbox Code Playgroud)

哪两个都返回相同的错误.为什么会这样?它看起来非常适合我.

Mar*_*rio 6

你确定你没有包括你的标题两次(没有使用#ifndef/ #pragma once避免这样做)?即使你的构造中存在一些错误,也不应该触发错误"重新定义'......'"因为它是第一行?

  • 请同时使用 - "#pragma`s是特定于编译器的. (2认同)