954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Declaring a Structure in a Header File

I'm trying to declare a structure definition in a header file so I can use the structure in other files, however I get the error:
error C2011: 'Points' : 'struct' type redefinition
from the file where the structure definition is declared. The structure is only defined in that header file.

This is all of the code in the header file:

struct Points {
int xCoordinate; 
int yCoordinate; 
int zCoordinate;
}; // end of struct Points definition


The name of the header file is StructPoints.h and there is no accompanying C++ file.

Also, this header is used in other files source and header files. If I include the header in a file called Test.h, would I also need to include it in a file called Test.cpp?

And is there anything else I need to add to make this header work?

TheBattlizer
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

You might want to put code guards in that header file to prevent the problem you describe.

#ifndef POINTS_H
#define POINTS_H
struct Points {
int xCoordinate; 
int yCoordinate; 
int zCoordinate;
}; // end of struct Points definition
#endif
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Dear TheBattlizer,
I think you could declare 2 structures with same name. If you include Test.h into file Test.cpp, you don't need to include StructPoints.h into file Test.cpp when it included into file Test.h

ttqtrang146
Newbie Poster
11 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

Try to understand the standard compilation process logic.

Simplified translation process description:

1. Every .cpp file is processed separately (as if no other cpp files)

2. Preprocessor stage: process all # directives. Every active (see AD's post, #ifdef/#ifndef directives) #include directive in .cpp file is replaced by referred .h (or other) file or header (named in angle brackets) contents (recursively, until all includes are replaced)

3. This is a translation unit text. It's compiled.

Now consider your case. You have Points definition in structpoints.h file. You have #include "structpoints.h" in test.h file. You have #include "structpoints.h" and #include "test.h" directives in test.cpp. Now try to compile test.cpp - see points above:

1. Get test.cpp
2. Replace #include "structpoints.h" . It's Points definition. Replace #include "test.h" to its contents and recursively replace yet another #include "structpoints.h" ! That's yet another Points definition in this translation unit - you breaks "one definition" rule.

No magics...

Now try to understand AD's solution of the problem.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You