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

  • Context: C/C++ 
  • Thread starter Thread starter Sam Groves
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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?
 
on Phys.org
You can use the pragma once idiom or some of the other methods shown on that page.
 
Reply
  • Like
Likes   Reactions: FactChecker