can two classes see each other using C++? - c++

So I have a class A, where I want to call some class B functions. So I include "b.h". But, in class B, I want to call a class A function. If I include "a.h", it ends up in an infinite loop, right? What can I do about it?

Put only member function declarations in header (.h) files, and put member function definitions in implementation (.cpp) files. Then your header files do not need to include each other, and you can include both headers in either implementation file.
For cases when you need to reference the other class in member signatures as well, you can use a forward declaration:
class A;
This lets you use pointer and reference types (A* and A&), though not A itself. It also doesn't let you call members.
Example:
// a.h
struct B; // forward declaration
struct A {
void foo(B* b); // pointers and references to forward-declared classes are ok
};
// b.h
struct A; // forward declaration
struct B {
void bar(A& a); // pointers and references to forward-declared classes are ok
};
// a.cpp
#include "a.h"
#include "b.h"
void A::foo(B* b) {
b->bar(*this); // full declaration of B visible, ok to call members now
}
// b.cpp
#include "a.h"
#include "b.h"
void B::bar(A& a) {
a.foo(this); // full declaration of A visible, ok to call members now
}

Each class (A and B) should have a header file and an implementation file.
Each header file (e.g. A.h) should not include the other header file (e.g. B.h) but may include a forward reference to the other class (e.g. a statement like class B;), and may then use pointers and/or references to the other class in its declaration (e.g. class A may contain a B* as a data member and/or as a method parameter).
Each CPP file (e.g. A.cpp) may include more than one header file (e.g. A.h and B.h). It's recommended that each CPP file should include its own header file first (e.g. A.cpp should include A.h and then B.h, whereas B.cpp should include B.h and then A.h).
Each header file should contain only the declaration, and not the definition of the class: for example it will list the signatures of the class' methods, but not the method bodies/implementations (the method bodies/implementations will be in the .cpp file, not in the header file). Because the header files don't contain implemention details, they therefore don't depend on (don't need to see) details of other classes; at most they need to know that, for example, B is the name of a class: which it can get from a forward declaratin, instead of by including a header file in another header file.

You can also use forward declarations to get around the issue.

Try putting #ifndef, #define and #endif around your .h files.

Related

How files will be loaded in Visual Studio C++ when built

I have three files.
Wood.cpp
Brick.cpp
Wall.cpp
And my main() function is in Brick.cpp:
Now, when I run the project, it throws error that the className (which is in Wall.cpp) is undefined. What should I do?
I think the main() function is run before the delaration of Wall.cpp file.
You need a way to tell the c++ compiler what function would be available and implemented, in C++ the way is using headers files:
Example:
File a.hpp
class A {
// variable members
// function signatures or inline functions
};
File a.cpp
// Implementation of functions in class A and initialization of static variables in A
File b.hpp
#include "a.hpp"
// Could use class A
class B {
A m_a_member_variable;
}
File b.cpp
// Implementation of functions in class B and initialization of static variables in B that could use classes and declarations in a.hpp (ex: class A)
I think the main() function is run before the delaration of Wall.cpp file. there is no run before while compiling. also, the compilation order is defined by the compiler.
you need to include the header (ussualy *.h, *.hpp, *.hh) file (if you have one, otherwise you have to create one) where className is defined into the source file where you use this class.
for the compiler it is enough to know the declaration (which is in the header) of a class to compile the source. the declaration is then necessary for the linker.

Stacking multiple forward declarations in one header and one namespace

I have a set of classes to implement and I plan to do it in the following way:
(1) A master header file (entity.h) that has all the forward declarations of these classes enclosed in a namespace i.e.:
namespace SomeNameSpace {
class A;
...
class E;
}
(2) Each of the classes then have separate header files (i.e. A.h, B.h) that defines the classes and declares all the constituent variables and methods.
(3) And finally, the .cpp's that define all the members and statics (i.e. A.cpp, B.cpp).
Now, I tried to put this scheme to work by doing #include "entity.h", but I am slapped with an "ambiguity" error whenever I try to use one of these classes. If I skip step (1) and simply include all the individuals header files of each class, then I don't have that problem (and that's how I've been writing code).
So anyway, is my scheme viable? Is there some way to make it work? What I really wanted is to have some sort of way to put every custom object in one namespace. I suppose I can make that work by simply putting all the class declarations into one file, but that'd make entity.h bloated.
I've searched around with keywords "forward declaration", "C++", "namespace", "one namespace", "master header", "header" and couldn't find anything that's quite relevant. Maybe what I want to do is wrong?
You cannot use "class A;" in your entity.h because that means that you are actually declaring a class but you already have done so in A.h
You can either add the whole declaration in your entity.h but not using any A.h, B.h etc.
Or, you can just use the same namespace in each *.h file :
A.h ->
namespace SomeNamespace {
class A ...
}
B.h ->
namespace SomeNamespace{
class B ...
}
A different approach would be wrapping the classes into a namespace directly from the original headers, and then including them, e.g.:
A.h:
namespace SomeNameSpace {
class A{
// Class definition
void something();
}
}
A.cpp:
#include "A.h"
namespace SomeNameSpace {
void A::something()
{
}
// And other function definitions
}
entity.h:
#include "A.h"
#include "B.h"
// And so on...
You cannot use a class in a scope where there is not a complete definition (the forward declaration is not enough). Normally you might forward declare in the header and #include in the cpp. I don't see the point in organizing the way you are trying to; The canonical wisdom is to forward declare whenever possible and #include whenever necessary.

C++ friend function references

I have this situation (two classes with two different header files):
b.h
#include "a.h"
class B {
friend void B* A::create(void);
private:
int x;
};
a.h
#include "b.h"
class A {
public:
void B* create(void);
...
};
basically class A creates "B" objects. I want to give the creation function create() access to modify the private member 'x' of class B. I want to simulate the 'internal' keyword in C#, but at a more granular level.
The problem I have is that I end up with a circular header reference. b.h wants to come after a.h, but a.h wants to come after b.h. What is the proper way of doing this? Do I need to resort to void* ptrs? Since I only create one type of object, i don't think I need the factory pattern. Hopefully I'm missing something simple.
What you need is a "forward declaration". Basically you don't include the classes' headers in other classes' headers (unless you want to use composition, then you have to). In your case, it'd look like this:
// B.h
class A;
class B {
// declare pointers to A freely
};
And the other way round in A.h.
You'd probably need to include A.h in B.cpp (and the other way round) to be able to actually dereference those pointers to access fields, but your problem with circular header inclusion is now gone.

Cross dependencies without forward declaring all used functions?

I have class A (in A.h) which depends on class B in (B.h) and vice versa. Forward declaring the used functions works, but this means I have to update everywhere where I forward declared those functions in the future, ex, if I remove, or change argument in those functions they must all be updated to reflect change. I don't feel this is good practice. Is there a way around this?
Thanks
If you only need to work with pointers or references to a class at the declaration level, you can do it like this:
A.h
class B; // forward class declaration
class A {
A(B &);
};
B.h
class A;
class B {
B(A &);
};
B.cpp
#include "B.h"
#include "A.h" // now we get the full declaration of A
B::B(A &a) {
a.foo(5);
}
Mutual dependencies like this are tough to deal with but sometimes unavoidable.
If A and B depend on the implementations of each other, then you've got a system design problem that you need to resolve before proceeding further.
The best way is to have a forward declaration header:
a.fwd.h
#pragma once
class A;
a.h
#pragma once
#include "a.fwd.h"
#include "b.fwd.h"
class A
{
A(B*);
};
etc.
This way, each class provides its own forward declarations - localised alongside the header where it belongs - checked for consistency with the real declarations and definitions by including the forward declaration header in the header, and the header in the implementation.

Auto-needed classes?

Suppose I have a class A and a class B.
The .h of A, needs the .h of B, and the .h of B needs the .h of A. (need = #include).
All .h have the guards:
#ifndef _classX_
#define _classX_
...
...
#endif
But if I compile the .cpp of A, then when it includes the .h of B, the B class cannot include the .h of A class because the A class has already use the guard.
How can i solve this?
You will need a forward declaration of one of the classes.
// a.h
// do not #include b.h
class B; // forward declaration
class A {
....
B * b;
};
Note that the class A cannot contain an actual B instance - it must be a pointer or a reference. You also cannot call any functions via the B pointer in the header - they will have to go in a .cpp source file which #includes both headers.
One of them need to get around not to include it. For many cases this is possible such that you can move the #include into the .cpp file
class A {
// no need for header of B here
void f(B b);
// no need for header of B here either
B *b;
};
Generally for the function declarations whose definitions are in the .cpp files, you don't need any #includes in the header. For class members you only need headers if you want to have them as value objects embedded. You need to change those to pointers (or smart-pointers) and new them within the .cpp file in that case. For the compiler to know what B is in my above example, you just need to put a forward declaration before the definition of A like this:
class B;