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

  • Thread starter Thread starter Sam Groves
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
The discussion centers on a compilation error arising from including both Activity.h and Stack.h in a program that utilizes a Stack class and an Activity class. The error indicates that Activity.h is defined multiple times. To resolve this issue, the recommended solutions include using the pragma once directive or employing include guards with the #ifndef preprocessor directive. These methods prevent multiple inclusions of the same header file, thus eliminating the compilation error.
Sam Groves
Messages
11
Reaction score
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
You can use the pragma once idiom or some of the other methods shown on that page.
 
  • Like
Likes FactChecker
From a C standpoint, I would use #ifndef <Some variable defined in the header you are trying to include>.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
5K
Replies
81
Views
7K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K