简体   繁体   中英

C typedef struct uncertainty

Consider the following typedef struct in C:

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;

I get the error:
globals.h:21: error: redefinition of 'struct source'
globals.h:28: error: conflicting types for 'source_t'
globals.h:28: note: previous declaration of 'source_t' was here

I've tried using other formats for this definition:


struct source{
...
};
typedef struct source source_t;

and


typedef struct{
...
}source_t;

Which both return the same error. Why does this happen? it looks perfectly right to me.

Are you sure you didn't include your header twice (without using #ifndef / #pragma once to avoid that)? Even if there'd been some mistake in your construct it shouldn't trigger the error "redefinition of '...'" cause it's the very first line?

The most likely cause is that your header file is being included more than once. You need to ensure that if this happens, the typedef is only executed once.

You can do this by wrapping globals.h with:

  #ifndef _globals_h_
  #define _globals_h_
  [...]
  #endif

The errors say a struct source has been defined more than once.

Maybe you included the header file twice?

Just to be on the safe side, be sure that your header gets only included once: put

#ifndef YOUR_HEADER_FILE_NAME
#define YOUR_HEADER_FILE_NAME

at the beginning, and

#endif

at the end of your header file: this will prevent it to be included twice or more by any source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM