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>.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

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