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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:47:57+00:00 2026-05-11T02:47:57+00:00

I have a problem which I think is related to forward declarations, but perhaps

  • 0

I have a problem which I think is related to forward declarations, but perhaps not.

Here is the relevant code:

A.h

#ifndef A_H_ #define A_H_  #include "B.h"  class A {     private:         B b;      public:         A() : b(*this) {}              void bar() {} };  #endif /*A_H_*/ 

B.h

#ifndef B_H_ #define B_H_  #include "A.h"  class A;  class B {     private:         A& a;      public:         B(A& a) : a(a) {}              void foo() { /*a.bar();*/ } //doesn't compile };  #endif /*B_H_*/ 

main.cpp

#include "A.h"  int main() {     A a;      return 0; } 

The problem seems to be with the invocation of A::bar(). The program successfully compiles until I attempt to call this method at which point I get two errors:

error: invalid use of incomplete type ‘struct A’

error: forward declaration of ‘struct A’

I presume this is because A::bar() has yet to be defined or declared since both headers reference each other. However, I forward declared class A and am at a loss as to what else I need to do. I am new to C++, so please forgive me. I could not find the answer to this question anywhere else online. As always, thanks in advance!

  • 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-11T02:47:58+00:00Added an answer on May 11, 2026 at 2:47 am

    You’ve got a circular reference, so you need to separate B.h. Try something like:

    B.h:

    #ifndef B_H_ #define B_H_  // don't include A.h here!  class A;  class B {    private:       A& a;     public:       B(A& a) : a(a) {}        void foo(); };  #endif /*B_H_*/ 

    B.cpp:

    #include 'B.h' #include 'A.h'  void B::foo() { a.bar(); } // now you're ok 

    Edit: explanation for why you need to split it into two files:

    The class B contains a reference to A, which can be a so-called incomplete type. You can’t call any functions on it because the compiler doesn’t yet know what the heck A is – it just knows that it’s a class of some sort. Once you include A.h (in the .cpp file), then A is a complete type, and you can do whatever you like with it.

    You can’t keep the whole thing in one header file because you’ll get a circular reference. You’re preventing an infinite loop with your include guards, but you’re getting something you don’t want. Look at what the compiler ends up with when you compile main.cpp, as you had it before:

    // #include 'A.h' ==> #define A_H_  // #include 'B.h' ==> #define B_H_  // #include 'A.h' ==> nothing happens! (since A_H_ is already defined)  class A;  class B { private:     A& a;  public:     B(A& a) : a(a) {}      void foo() { a.bar(); } // <-- what the heck is A here?                             //     it's not defined until below };  class A { private:    B b;  public:    A() : b(*this) {}     void bar() {} };  int main() {     A a;     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • 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
  • added an answer Your example isn't clear, and doesn't (IMO) show typical capture… May 11, 2026 at 2:44 pm
  • added an answer Delphi documentation tools Using the XMLDoc tool for API documentation… May 11, 2026 at 2:44 pm
  • added an answer Yes, they will usually be identical. However, if you're working… May 11, 2026 at 2:44 pm

Related Questions

I have a JFrame that contains a display JPanel with JTextField and a control
This I think is related to my use of the nlog C++ API (and
I have two issues both related to (I believe) my SQL Server setup. I
I know this question has been asked a bit before. But looking around I

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.