Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:10:36+00:00 2026-06-05T12:10:36+00:00

In C++, I have some header files such as: Base.h , and some classes

  • 0

In C++, I have some header files such as: Base.h, and some classes using Base.h:

//OtherBase1.h
#include "Base.h"
class OtherBase1{
    // something goes here
};

//OtherBase2.h
#include "Base.h"
class OtherBase2{
    // something goes here
};

And in main.cpp, I can only use one of those two OtherBase classes because of duplicate header. If I want to use both classes, in OtherBase2.h I have to #include "OtherBase1.h" instead of #include "Base.h". Sometimes, I just want to use OtherBase2.h and not OtherBase1.h, so I think it’s really weird to include OtherBase1.h in OtherBase2.h. What do I do to avoid this situation and what’s the best practice for including header file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:10:38+00:00Added an answer on June 5, 2026 at 12:10 pm

    You should be using include guards in Base.h.

    An example:

    // Base.h
    #ifndef BASE_H
    #define BASE_H
    
    // Base.h contents...
    
    #endif // BASE_H
    

    This will prevent multiple-inclusion of Base.h, and you can use both OtherBase headers. The OtherBase headers could also use include guards.

    The constants themselves can also be useful for the conditional compilation of code based on the availability of the API defined in a certain header.

    Alternative: #pragma once

    Note that #pragma once can be used to accomplish the same thing, without some of the problems associated with user-created #define constants, e.g. name-collisions, and the minor annoyances of occasionally typing #ifdef instead of #ifndef, or neglecting to close the condition.

    #pragma once is usually available but include guards are always available. In fact you’ll often see code of the form:

    // Base.h
    #pragma once
    
    #ifndef BASE_H
    #define BASE_H
    
    // Base.h contents...
    
    #endif // BASE_H
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using Eclipse+CDT+SVN(Linux). How can we have some common header files in a
I have some external header files in a separate directory (its the sqlite3.h). I
I have this header file, zeeheader.h, and I wrote some classes in it, I'm
How to load/render a usercontrol using jquery? I have some menus on header of
I have a java web application. Here I have some javascript files which I
I have few header files in /my/path/to/file folder. I know how to include these
I have some useful typedefs on a header file called utypes.h. I have decided
I have some extern 'd variables in a namespace in a header file, and
I have an XML Template file. This file contains a header and some predefined
I have some VERY simple code to return the title for a section header:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.