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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:52:46+00:00 2026-05-27T07:52:46+00:00

I have found a similar question here , but my intent is little different.

  • 0

I have found a similar question here, but my intent is little different.

class B is the embedding class while class A is the embedded class. I want to let B::A have access to member functions of class B. I have seen compilation errors through g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2. The detailed errors are as follows:

~/Documents/C++ $ g++ embed.cpp 
embed.cpp:5:7: error: ‘B’ has not been declared
embed.cpp: In constructor ‘B::B()’:
embed.cpp:10:27: error: invalid use of incomplete type ‘struct B::A’
embed.cpp:14:9: error: forward declaration of ‘struct B::A’ 

Is there a way that I can make it work?
Thank you

#include <iostream>
#include <string>
using namespace std;

class B
{
public:
  B() : impl(new B::A(this)) {}
  ~B(){}

private:
  class A; // want to hide the implementation of A
  A* impl;
};

class B::A
{
public:
  A(B* _parent) : parent(_parent) {} // let embedded class A has access to this parent class
  ~A() { parent = NULL; }

  B* parent;
};

int main(void)
{
  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. Editorial Team
    Editorial Team
    2026-05-27T07:52:46+00:00Added an answer on May 27, 2026 at 7:52 am

    This problem is solved easily if you follow the convention of having the header in one file and the implementation in another.

    In file b.h:

    class B
    {
    public:
      B();
      ~B();
    
    private:
      class A; // want to hide the implementation of A
      A* impl;
    };
    
    class B::A
    {
    public:
      A(B* _parent);
      ~A();
    
      B* parent;
    };
    

    In file b.cpp:

    B::B(void)
        :impl(new A(this))
    {
    
    }
    
    //other methods and such
    

    My compiler was giving me a different error: in the constructor for B, you were default-constructing an object that had no default constructor (because it’s an incomplete class). The solution is to implement the B constructor after class A has been fully defined, and the header/implementation separation is a natural way to achieve that.

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

Sidebar

Related Questions

I found a similar question here, but didn't help me. I basically have apache
For the record: I found a similar question here but I have to elaborate
I know a similar question has been asked but I have not found a
Morning, simple stupid question. I have found post with similar issues but after reading
I found a similar question here but wasn't exact to my scenario. I'm relatively
I can't make it work. I found many similar question here, examples. But nothing
I have asked a similar question before here , but after much thought, and
I found a similar question about this but aimed at Debian here . However
The only similar question I have found is: Insert php boolean into mysql bit
I know that this is a repeated question. I have found very similar questions

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.