Reputation: 6801
I have a Base class Base
.
Two classes that derive from it: DerivedA
and DerivedB
. In headers of both classes I included Base.h
.
Then, in my source.cpp
where I define my main
, if I ONLY include DerivedA.h
, it works fine. If I ONLY include DerivedB.h
it also works fine. The problem is that I can't include BOTH.
Whenever I include both, the compiler generate a bunch of errors like
I guess the problem is that when I include both header, the Base.h
is included twice. How should I include these headers?
Upvotes: 2
Views: 2613
Reputation: 11439
If the problem is just the header file and not a case of the deadly diamond of death, you could just use #pragma once
in your header file and that will ensure it's only ever linked once during the build process.
Alternatively, you could wrap your header file in something like this:
#ifndef HEADER_H_
#define HEADER_H_
// The content of the header file goes here...
#endif
By using pre-processor #ifXXXX
statements, you can tell the compiler to only include the file if a macro is not already pre-defined.
Upvotes: 7
Reputation: 577
When you are inheriting from two classes which are from the same base, you should try to use the keyword "virtual" when doing the inheritance. It helps you to solve the confusion in the virtual table I believe. Look up virtual inheritance for examples.
Upvotes: -1