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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:44:56+00:00 2026-06-11T04:44:56+00:00

Here is what I am trying to do, I want the class A’s constructor

  • 0

Here is what I am trying to do, I want the class A’s constructor to call B’s constructor for an object of B, like this main()->A->B

A.cpp:(included B.h)

A::A(){
  B foo;
}

B.h:

class B{
 B(){//some code};
};

but GCC won’t compile and says A::B foo has initializer but incomplete type. I am guessing the compiler saw no local class of B defined in A, so it complained and didn’t know the Class B was from another file. My question is how to construct an B’s object in A’s constructor like above.I am sure I am missing some fundamentals about C++, please bear with me. Thank you 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. Editorial Team
    Editorial Team
    2026-06-11T04:44:57+00:00Added an answer on June 11, 2026 at 4:44 am

    You do not have any class of type A::B. From your comment, it looks like you are trying to use a pointer to B by calling it A::B *. This is incorrect. A pointer to B is always B *, regardless of where it appears. From what you said, it looks like you want something like this:

    a.hpp

    #ifndef A_HPP_
    #define A_HPP_
    
    class B;
    class A {
    public:
        A(B * b);
    private:
        B * my_very_own_b;
    };
    
    #endif    // A_HPP_
    

    a.cpp

    #include "a.hpp"
    #include "b.hpp"
    
    A::A(B * b):
        my_very_own_b(b)
        {
    }
    

    b.hpp

    #ifndef B_HPP_
    #define B_HPP_
    
    class B {
    public:
        B();
    private:
        int x;
    };
    
    #endif    // B_HPP_
    

    b.cpp

    #include "b.hpp"
    B::B():
        x(0)
        {
    }
    

    main.cpp

    #include "a.hpp"
    #include "b.hpp"
    
    int main() {
        B b;
        A a(&b);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here I am trying to call overloaded constructor after some condition has met in
Ok here's what I'm trying to do I want to write a class that
I'm trying to mix a module into a class, and I want some of
Here's what I'm trying to do: I want to get a list of all
Related question [stackoverflow] here . I'm trying to do the above, but I want
Here I am trying to implement the jQuery I have two main files one
Instead of trying to trying to put my problem into words, here's some code
Here are the values I want to get in another class. @Override protected void
Here's the coding problem I am trying to solve... I have a base class,
I am trying to create an class object to a class but I keep

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.