|
-
May 10th, 2011, 10:20 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] redefinitions abound
Hi,
I'm showing my inexperience here, but I believe I have made a mess of includes and I'm wondering if someone can give me some pointers. Here's the situation. I've written programs in C for a little while now, but none this complex, that require so many different files in the project.
I have a file, called CustomTypes.h, that holds a number of structures and enums. I have a main file that calls a function in a display file. When main calls the display function, it passes a structure to it for display. Problem is, main has to include CustomTypes.h so it knows what the structure is. The display function also includes it so it knows what the structure is. That is, it is defined in the display function prototype, which is located in the display header file. Now, since both of these include the same CustomType.h, I am getting errors at compile time stating that the structures and enums inside CustomType.h are being redefined. How is this mess typically kept straight?
Thanks.
Last edited by rickford66; May 10th, 2011 at 12:43 PM.
-
May 10th, 2011, 11:54 AM
#2
Re: redefinitions abound
This is what header guards are for:
Code:
#ifndef __CUSTOM_TYPES_H__
#define __CUSTOM_TYPES_H__
// Your header definitions goes here.
#endif
This will make sure that your header definitions will only be defined once, even if the file is included at multiple locations.
-
May 10th, 2011, 12:23 PM
#3
Thread Starter
Fanatic Member
Re: redefinitions abound
That makes sense. I tried it, but I still get the errors. Do I have a syntax error here?
Code:
#ifndef SCREEN_STATE
typedef enum SCREEN_STATE
{
CREATE_POWER_UP_SCREEN,
SHOW_POWER_UP_SCREEN,
CREATE_SENSOR_CAPACITY_SCREEN,
SHOW_SENSOR_CAPACITY_SCREEN,
CREATE_MAIN_DISPLAY_SCREEN,
SHOW_MAIN_DISPLAY_SCREEN,
CREATE_MAIN_MENU_SCREEN,
SHOW_MAIN_MENU_SCREEN,
CREATE_MODE_SCREEN,
SHOW_MODE_SCREEN,
};
#endif
-
May 10th, 2011, 12:31 PM
#4
Re: redefinitions abound
There's no syntax error, but you've got it a bit wrong.
The ifndef directive checks if a preprocessor directive is defined or not. You need to add a #define inside, the enum will not make a difference for the #ifndef directive.
Also, this might just be a small subset of your code that you've simplified, but you should really wrap your entire header in one big header guard.
-
May 10th, 2011, 12:40 PM
#5
Thread Starter
Fanatic Member
Re: redefinitions abound
I think I got it now. Thanks.
Code:
#ifndef _CUSTOM_TYPES
#define _CUSTOM_TYPES
//header definitions go here
#endif
-
May 10th, 2011, 12:41 PM
#6
Thread Starter
Fanatic Member
Re: redefinitions abound
Which is exactly what you posted earlier. haha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|