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

  • Home
  • SEARCH
  • 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 6766997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:53:54+00:00 2026-05-26T14:53:54+00:00

Possible Duplicate: C header file loops Original Question: I always had problems understanding why

  • 0

Possible Duplicate:
C header file loops

Original Question:

I always had problems understanding why the following gives errors:

something.h

#ifndef SOMETHING_H
#define SOMETHING_H

#include "somethingelse.h"

#endif

somethingelse.h

#ifndef SOMETHINGELSE_H
#define SOMETHINGELSE_H

#include "something.h"

#endif

Why does this give errors?

1) SOMETHING_H is not defined

2) SOMETHING_H becomes defined, somethingelse.h get included

3) SOMETHINGELSE_H is not defined, becomes defined, and something.h gets included

4) SOMETHING_H is defined, jump to #endif, this should be the end of it?


EDIT:

turns out it doesn’t give any errors at all. However the following does:

something.h

#pragma once

#include "somethingelse.h"

class something {
    int y;
    somethingelse b;
};

somethingelse.h

#pragma once

#include "something.h"

class somethingelse {
    something b;
    int x;
};

And it is logical, because the class ‘something’ is not yet defined when ‘somethingelse’ needs an instance of that class.

The problem is solved by forward definition:

something.h

#pragma once

class somethingelse;

class something {
    int y;
    somethingelse* pB; //note the pointer. Correct me if I'm wrong but I think it cannot be an object because the class is only declared, not defined.
};

in the .cpp, you can include “somethingelse.h”, and make instances of the class.

  • 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-05-26T14:53:55+00:00Added an answer on May 26, 2026 at 2:53 pm

    Your description is exactly correct, except that there is no error at all. Add pragma message("Some text") (assuming Visual Studio) at various places to trace the flow.

    As other posters have already noted, your header files most likely contain classes that mutually require each other’s definition, and that is the cause of the problem. This sort of problem is usually solved by

    • Using forward references where possible
    • Moving #includes to CPP files where possible
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why can templates only be implemented in the header file? I've been
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: What's the difference between a header file and a library? Can anyone
Possible Duplicate: Declaring a function that return a 2D array in a header file?
Possible Duplicate: Why can templates only be implemented in the header file? I've struggled
Possible Duplicate: Is there a shortcut to move between header and source file in
Possible Duplicate: Why can templates only be implemented in the header file? hello everyone
Possible Duplicate: Detect if PDF file is correct (header PDF) I want to validate
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there

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.