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++
AI Thread 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>.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top