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

The Archive Base Latest Questions

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

Here is the problem. I need to write header file with two classes, say

  • 0

Here is the problem. I need to write header file with two classes, say class A and class B.

in class A I have function that uses object of class B and vice versa, i.e. in class B I have function that uses objects of class A.

If A declared first then there would be error that class B has not been declared.
How to deal with it? I try declare function of a class A after declaration of class B:

void classA::myfunc (classB *b);

But I got the error that function myfunc is not declared.
Experienced people in C++, what to do?

Added:
here is a good link about header

  • 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-27T16:34:52+00:00Added an answer on May 27, 2026 at 4:34 pm

    If you need a pointer to a class on a header, not the full object, just add a forward declaration, dont include the header of the pointer’s class.

    I’m sure that you just use pointers to access that classes that one have a reference to another, dont you? You know, because if you use instances, you got a instance looping. Use forward declarations.

    Here’s a example of how you can use forward declarations:

    A.h

    class B;
    class C;
    class D;
    class E;
    
    class A {
        B* pointer; //To just have a pointer to another object.
        void doThings(C* object); //if you just want to tell that a pointer of a object is a param
        D* getThings(); //if you wanna tell that a pointer of such class is a return.
        E invalid(); //This will cause an error, because you cant use forward declarations for full objects, only pointers. For this, you have to use #include "E.h".
    };
    

    To illustrate how can have a class that mentions one that pointers its type:

    B.h

    class A;
    class B {
        A* pointer; //That can be done! But if you use a includes instead of the forward declarations, you'll have a include looping, since A includes B, and B includes A.
    }
    

    As mentioned by Tony Delroy (Many thanks to him) You should not ALWAYS use this design. It’s provided by the C++ compiler, but its not a good practice. The best is to provide reference header, so your code would look like:

    A.h

    #include "B.fwd.h"
    #include "C.fwd.h"
    #include "D.fwd.h"
    #include "E.fwd.h"
    
    class A {
        B* pointer; //To just have a pointer to another object.
        void doThings(C* object); //if you just want to tell that a pointer of a object is a param
        D* getThings(); //if you wanna tell that a pointer of such class is a return.
        E invalid(); //This will cause an error, because you cant use forward declarations for full objects, only pointers. For this, you have to use #include "E.h".
    };
    

    and yours forward headers like this:

    B.fwd.h:

    class B;
    

    In your fwds, you should have your class forward declaration, and any typedefs that comes with it.

    I’m not mentioning the #pragma once, or the #ifndef B.H... you know they’ll be there 😀

    Your code would be on a standard defined by <iosfwd> and better to maintain, specially, if they are templates.

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

Sidebar

Related Questions

Ok, so here's the problem I have to solve. I need to write a
I have an interesting SQL problem that I need help with. Here is the
I need a little push in the right direction. Here's my problem: I have
This is my first post here. I have a problem. I need to take
The setup: I have a standard .php file (index.php) that contains two includes, one
Still working on a problem that started from here Calling C++ dll function from
I need to write an excel-like grid that can have a lot of cells
Well i need some help here i don't know how to solve this problem.
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
I have a problem here. My Zend_Forms do not render in view script. Via

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.