How to avoid compilation errors with multiple header files in C++?

  • Thread starter Sam Groves
  • Start date
  • Tags
    C++
  • #1
Sam Groves
11
0
I have a class Stack with a header file Stack.h which uses up another class with a header file Activity.h

I have a third class ActivityManager which uses up both the Stack and the Activity class.If I run the program and add in both Activity.h and Stack.h to the #include list I get a compilation error:Activity.h is defined 2 times in the script.How do I solve this issue?
 
Technology news on Phys.org
  • #2
You can use the pragma once idiom or some of the other methods shown on that page.
 
  • Like
Likes FactChecker
  • #3
From a C standpoint, I would use #ifndef <Some variable defined in the header you are trying to include>.
 

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
428
  • Programming and Computer Science
Replies
6
Views
947
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
2
Views
364
Back
Top