Dave Sinkula
long time no c
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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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