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 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

Related Questions

I have the following problem, which I think is related to caching but I
I have a problem which makes me crazy. I think it is very easy
I have a peculiar problem, which I think that a lot of other people
I'm developing an Eclipse plug-in and I have a problem which is reproduced here:
I have a pretty weird problem in my iPhone app which is, I think,
I have came across very interesting problem which I think it's on Salesforce's end
I think this is a scope related problem. If I have a rule on
I have an interesting (but frustraring) problem. I have an application which uses the
I actually have two questions regarding the same problem but I think it is
I have a problem which I don't really know how to solve. I have

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.