ashim
ashim

Reputation: 25560

about class declaration in header file C++

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

Upvotes: 3

Views: 13732

Answers (3)

boydc
boydc

Reputation: 11

You can try to add a forward declaration of a class before you use it with a ref or ptr.

classA.h:

class classB;

class classA {
    void fun(classB * b);
}

classB.h:

class classA;

class classB {
    void fun(classA * a);
}

classA.cpp:

#include "classA.h"
#include "classB.h"

...

classB.cpp:

#include "classA.h"
#include "classB.h"

...

Upvotes: 0

Gustavo Maciel
Gustavo Maciel

Reputation: 652

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 :D

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

Upvotes: 10

Jaffa
Jaffa

Reputation: 12700

Short answer:

class classA;

Then you defined your classB and then the declaration of classA.

This is called forward-declaration, and is there to solve your problem :)

Upvotes: 4

Related Questions