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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:03:59+00:00 2026-06-09T21:03:59+00:00

Code looks like this: class B {/*something*/}; class D1: public B { public: void

  • 0

Code looks like this:

class B {/*something*/};

class D1: public B {
public:
void set() = 0;
friend int D2::get(D1&);
private: 
int a;
}
class D2: public B {
public:
int get(D1& d) {return d.a;}
}

I’ve included "B.h" in both derived class’ .h files("D1.h" and "D2.h") and also “D2.h” in D1, "D1.h" in D2…but keep getting compiling error:

...\D2.h ... use of undefined type D1

So what I’m doing wrong? Thanks.

  • 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-06-09T21:04:00+00:00Added an answer on June 9, 2026 at 9:04 pm

    You created a circular inclusion: you included D1.h into D2.h and you also included D2.h into D1.h. Circular inclusion never works and never achieves anything.

    Your circular inclusion is a direct consequence of circular dependency between your class definitions. In your code both class definitions refer to each other in a way that requires both class types to be complete (to be completely defined). This means that your code, as is, cannot be compiled, regardless of what you do. You need to break the circular dependency between class definitions.

    In your case it can be done in the following way.

    Keep D1.h unchanged, i.e. keep including D2.h into D1.h and keep the definition of class D1 as is.

    However, do not include D1.h into D2.h Instead introduce a forward declaration of D1 into D2.h

    class D1;
    

    Change the definition of D2 to

    class D2: public B {
    public:
      int get(D1& d);
    };
    

    Note: do not attempt to define method get right in the definition of class D2. You have to relocate the definition of D2::get to some other place, where the full definition of D1 is also visible (like in D2.cpp for example, which should include both D1.h and D2.h)

    int D2::get(D1& d) 
    {
      return d.a;
    }
    

    That’s it. A side-effect of defining D2::get this way is that it becomes non-inline. If you really want to keep it inline, you’ll have to define it as

    inline int D2::get(D1& d) 
    {
      return d.a;
    }
    

    and also make sure it is somehow included only after the full definition of D1. You can place it, for example, into a third header file (D2_aux.h or something like that) and remember to include it after the D1.h.

    Of course, a better way to try to solve this issue is to rethink the entire design. Do you really need that friend declaration inside D1? Maybe you should somehow redesign your code to eliminate the need for that friend declaration and thus eliminate this dependency.


    Alternatively, you can resolve it by changing D1.h and keeping D2.h unchanged. However, to follow that path you’ll have to replace your “fine-grained” friend declaration

    friend int D2::get(D1&);
    

    with a more sweeping and permissive

    friend class D2;
    

    and remove the inclusion of D2.h from D1.h.

    The former friend declaration requires class D2 to be complete, which is actually what creates an “unbreakable” dependency. The latter declaration does not require D2 to be complete.

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

Sidebar

Related Questions

I have code that looks like this: template<class T> class list { public: class
The basic pseudo code looks like this: void myFunction() { int size = 10;
I've got a class something like this: public class Account { public virtual int
I have a class which looks something like this: class MyClass { public: //
I have code that looks like this: class T {}; class container { const
I've recently been working with code that looks like this: using namespace std; class
My code looks like this : Vector<String> My_Vector=new Vector<String>(); String My_Array[]=new String[100]; for (int
example: http://jsfiddle.net/kuTLf/ code looks like this: <div id=main> <div id=slideshow class=pics> <div id=nav></div> <img
I am maintaining some code which looks something like this. It's a Windows service
I have a web control that looks like this public class Foo : WebControl

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.