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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:23:35+00:00 2026-06-13T18:23:35+00:00

I have two classes which both need to have links to objects of one

  • 0

I have two classes which both need to have links to objects of one another. Here’s some example code to show my issue:

//object1.h
class object1{
    object2 *pointer;
}

My other class:

//object2.h
class object2{
    object1 *pointer;
}

I’m not exactly sure how I’m supposed include the two classes simultaneously in each other’s file. Having an include for the other class in both files is causing me issues. Preferably, I would like to just have both the objects in one file, since their headers are only a few lines of code each, but this causes an error on whichever class is first declared in the file, since the other class header isn’t preceeding it; It gives me an invalid type error.

  • 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-13T18:23:37+00:00Added an answer on June 13, 2026 at 6:23 pm

    Use a forward declaration:

    //object1.h
    class object2;
    class object1{
        object2 *pointer;
    };
    
    //object2.h
    class object1;
    class object2{
        object1 *pointer;
    };
    

    A forward declaration introduces an incomplete type.
    In short, it tells the compiler that the definition exists somewhere else.
    You have some limitations with incomplete types, though. Since the compiler doesn’t know its full definition, it cannot do things like allocating enough space for it, invoking member functions, checking the virtual table, etc.
    Fundamentally, what you can do is to just declare references/pointers to them and pass’em around:

    // Forward declaration
    class X;
    
    class Y {
    private:
    //  X x;    // Error: compiler can't figure out its size
        X &x;    // A reference is ok
    public:
        Y(X &x) : x(x) { }
    
    //  void foo() { x.foo(); } // Error: compiler can't invoke X's member
        void bar();
    };
    
    // Full declaration
    class X {
    public:
        void foo() { }
    };
    
    void Y::bar()
    {
        x.foo(); // Now it is OK to invoke X's member
    }
    

    A good pattern is to use forward declarations to break dependency cycles between headers or class definitions and make you wonder whatever your design could be improved.

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

Sidebar

Related Questions

I have some POCO classes which can generally divided into two groups, for example:
i have two data classes which hold only data members(no functions). One is CallTask
I have two classes one of which inherits from the other. Im trying to
I have two classes (MVC view model) which inherits from one abstract base class.
I have a class that instantiates two classes which implement interfaces. I want one
I have two C++ projects in a solution both of which need to share
I have a set of classes, each of which need to decide at some
I have two related classes which share a common interface and are both stored
I have two classes, a class1 which is an NSObject and a class2 which
Which I'm trying to accomplish is have two classes, a non generic and a

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.