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 6674961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:47:40+00:00 2026-05-26T03:47:40+00:00

Why do I need to use forward declarations for a class when I am

  • 0

Why do I need to use forward declarations for a class when I am already including the relevant header file? It has fixed my problem but confused me!

essentially class A needs to know about class B and visa-versa. I was getting the error “sytax error – identifier” before I included the forward declarations. I was under the impression including a header file essentially meant you WERE declaring the other class where you put #include.

  • 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-26T03:47:41+00:00Added an answer on May 26, 2026 at 3:47 am

    When you have a circular include dependency, and you’re properly using include guards (as you clearly are), then since there isn’t an infinite circular include, the compiler has to stop at some point, and it doesn’t know about the contents of the other header. When you add the forward declaration it’s able to compile your code.

    For example, A includes B. Then B includes A, but A‘ include guard kicks in and prevents it from being evaluated a second time. Then it drops back into B which doesn’t know about anything from A.h at that point because the include guard prevented its inclusion. The forward declaration however doesn’t have any include guards around it so it proceeds as normal.

    There’s nothing inherently wrong with circular dependencies, but I do always suggest taking a second look at your design to see if factoring out logic into a third class is possible. Forward declarations are the C++ mechanism to prevent infinitely circular dependencies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my C++ header files I try to use forward declarations (class MyClass;) instead
I have a problem which I think is related to forward declarations, but perhaps
I'm really confused with this problem. What I need to do is use some
I need to use an alias in the WHERE clause, but It keeps telling
I need to use googlemaps in my java application, but after some researches I
I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward
I need to use popen in a project, but I get: error: 'popen' was
I know that I can do: class Foo; but can I forward declare a
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need to use sed to convert all occurences of ##XXX## to ${XXX} .

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.