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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:43:00+00:00 2026-06-06T19:43:00+00:00

I was compiling the following code: class B; class A { A(); friend A

  • 0

I was compiling the following code:

class B;

class A {
    A();
    friend A B::newAObject();
};

class B {
    friend A::A();
public:
    A newAObject();
};

This may seem strange, but the idea was to have a class A that could only be produced by an object of type B (who would probably be a singleton).

The problem is that I’ve created a circular dependency between these objects. A must be defined before B, and B must be defined before A. Apparently forward declaring B is not good enough, B must actually be fully defined before A (and vice versa).

How do I get around this?

Edit: the actual error is: Incomplete type ‘B’ named in nested name specifier.

Note: there is another post that is similar to this here: Error: incomplete type used in nested name specifier, but it’s heavily templatized and that was confusing me, hence this post.

  • 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-06T19:43:02+00:00Added an answer on June 6, 2026 at 7:43 pm

    C++2003 states that when the contents of the class are accessed, this class should be fully defined. The forward declaration is not enough. This means that circular dependencies like yours are simply not allowed.

    p.s. Declaring the whole class as a friend should work, if this is all what you need.

    By the way, friend specification generates forward declaration for the class, look at the following code:

    void    F10(C1 &p1); 
    
    class C2 
    { 
        friend class C1; 
    }; 
    
    void    F11(C1 **p1);
    

    Compiler will give syntax error for F10 because C1 is undefined, but F11 will compile fine because of the friend spec. This may sound strange but this is defined in the standard and compilers follow this.

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

Sidebar

Related Questions

I have following code that I am compiling in a .NET 4.0 project namespace
I have the following code and am getting this error before compiling: Fast Enumeration
Hy i have the following code : import java.sql.*; import java.net.*; public class binsz
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
guys, I have the following code, but I'm getting error at compiling time... the
while compiling the following code i have got error @Override public void routeCustomerRequest (int
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
I am compiling following code and getting following error. How to fix this? Thanks
I'm trying to overload the dereference operator, but compiling the following code results in
I'm a novice at compiling with g++. But it's ok with the following code

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.