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

  • Home
  • SEARCH
  • 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 8413915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:01:47+00:00 2026-06-10T01:01:47+00:00

I have 2 header files that contain 2 classes. Each class is dependent on

  • 0

I have 2 header files that contain 2 classes. Each class is dependent on other, like so:

// class1.h

#include "class2.h"

class ClassOne {
    ClassTwo* c2;
};

// class2.h

#include "class1.h"

class ClassTwo {
    ClassOne* c1;
};

I expected the code not to compile, so I added a forward declaration to one of the headers:

// class1.h

#include "class2.h"

class ClassTwo;

class ClassOne {
    ClassTwo* c2;
};

But sadly that isn’t working, either. I keep getting compiler errors for “use of undefined type ‘ClassTwo'”.

I know I can just combine them into one header file and they will work, but in reality they are both really big classes and I really would prefer they have their own header file for organization purposes.

Is there a way to get around this?

Thanks,
Alex

  • 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-10T01:01:49+00:00Added an answer on June 10, 2026 at 1:01 am

    You added the forward declaration but didn’t remove the include. Do that and you’re golden.

    Actually, remove both includes, and replace them with forward declarations.

    // class1.h
    
    class ClassTwo;
    
    class ClassOne {
        ClassTwo* c2;
    };
    
    
    // class2.h
    
    class ClassOne;
    
    class ClassTwo {
        ClassOne* c1;
    };
    

    Note that cases where circular dependencies are actually required are few and far apart, so at least review the design.

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

Sidebar

Related Questions

Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
I have flv files that contain audio tags with a aac raw data. Each
I have a bunch of files that contain a semi-standard header. That is, the
I have the simplest code that I want to separate in three files: Header
I have been seeing code like this usually in the start of header files:
Ok I have two modules, each containing a class, the problem is their classes
If I have 2 header files, Test1.h and Test2.h, in which I define classes
I currently have a project with 3 files. One DBheader.h header file that includes:
I have a header file, lets say Common.h, that is included in all of
I have a c++ implementation file (my.cpp) that indirectly includes a header file (b.h)

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.