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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:54:43+00:00 2026-05-11T11:54:43+00:00

I often find myself in a situation where I am facing multiple compilation/linker errors

  • 0

I often find myself in a situation where I am facing multiple compilation/linker errors in a C++ project due to some bad design decisions (made by someone else 🙂 ) which lead to circular dependencies between C++ classes in different header files (can happen also in the same file). But fortunately(?) this doesn’t happen often enough for me to remember the solution to this problem for the next time it happens again.

So for the purposes of easy recall in the future I am going to post a representative problem and a solution along with it. Better solutions are of-course welcome.


  • A.h

    class B; class A {     int _val;     B *_b; public:      A(int val)         :_val(val)     {     }      void SetB(B *b)     {         _b = b;         _b->Print(); // COMPILER ERROR: C2027: use of undefined type 'B'     }      void Print()     {         cout<<'Type:A val='<<_val<<endl;     } }; 

  • B.h

    #include 'A.h' class B {     double _val;     A* _a; public:      B(double val)         :_val(val)     {     }      void SetA(A *a)     {         _a = a;         _a->Print();     }      void Print()     {         cout<<'Type:B val='<<_val<<endl;     } }; 

  • main.cpp

    #include 'B.h' #include <iostream>  int main(int argc, char* argv[]) {     A a(10);     B b(3.14);     a.Print();     a.SetB(&b);     b.Print();     b.SetA(&a);     return 0; } 
  • 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. 2026-05-11T11:54:44+00:00Added an answer on May 11, 2026 at 11:54 am

    The way to think about this is to ‘think like a compiler’.

    Imagine you are writing a compiler. And you see code like this.

    // file: A.h class A {   B _b; };  // file: B.h class B {   A _a; };  // file main.cc #include 'A.h' #include 'B.h' int main(...) {   A a; } 

    When you are compiling the .cc file (remember that the .cc and not the .h is the unit of compilation), you need to allocate space for object A. So, well, how much space then? Enough to store B! What’s the size of B then? Enough to store A! Oops.

    Clearly a circular reference that you must break.

    You can break it by allowing the compiler to instead reserve as much space as it knows about upfront – pointers and references, for example, will always be 32 or 64 bits (depending on the architecture) and so if you replaced (either one) by a pointer or reference, things would be great. Let’s say we replace in A:

    // file: A.h class A {   // both these are fine, so are various const versions of the same.   B& _b_ref;   B* _b_ptr; }; 

    Now things are better. Somewhat. main() still says:

    // file: main.cc #include 'A.h'  // <-- Houston, we have a problem 

    #include, for all extents and purposes (if you take the preprocessor out) just copies the file into the .cc. So really, the .cc looks like:

    // file: partially_pre_processed_main.cc class A {   B& _b_ref;   B* _b_ptr; }; #include 'B.h' int main (...) {   A a; } 

    You can see why the compiler can’t deal with this – it has no idea what B is – it has never even seen the symbol before.

    So let’s tell the compiler about B. This is known as a forward declaration, and is discussed further in this answer.

    // main.cc class B; #include 'A.h' #include 'B.h' int main (...) {   A a; } 

    This works. It is not great. But at this point you should have an understanding of the circular reference problem and what we did to ‘fix’ it, albeit the fix is bad.

    The reason this fix is bad is because the next person to #include 'A.h' will have to declare B before they can use it and will get a terrible #include error. So let’s move the declaration into A.h itself.

    // file: A.h class B; class A {   B* _b; // or any of the other variants. }; 

    And in B.h, at this point, you can just #include 'A.h' directly.

    // file: B.h #include 'A.h' class B {   // note that this is cool because the compiler knows by this time   // how much space A will need.   A _a;  } 

    HTH.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yeah it's using the wrong encoding. Check out http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html to… May 12, 2026 at 10:53 am
  • Editorial Team
    Editorial Team added an answer You selector is selecting all descendents. To select the immediate… May 12, 2026 at 10:53 am
  • Editorial Team
    Editorial Team added an answer You can't unload an individual assembly - you have to… May 12, 2026 at 10:53 am

Related Questions

I often find myself in a situation where I create a generic interface or
Is there a way to run a regexp-string replace on the current line in
I often find myself in the following situation: The customer reports corrupt data in
As a UI guy (coding and designing user interfaces) I often find myself in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.