Currently, I'm writing a project for fun and faced next problem.Let's suppose I'm creating class Bank and I want to define classes LoginController and Departure
class Bank{
class LoginController{
//some implemantation
}
class Departure{
//some departure
}
}
To avoid making huge declaration of Bank inside Bank.h I want to define them in separate files
//Bank.h
#include "LoginController.h"
#include "Departure"
class Bank{
class LoginController;
class Departure;
LoginController controller;
Departure departure;
...
}
//LoginController.h
class Bank; //forward declaration
class Bank::LoginController{
bool getAccess(Bank::Departure);
}
//Departure.h
//Departure depends on Bank class aswell
class Bank; //forward declartion
class Bank::Departure{...}
This code wont compile with huge stack trace.It can be solved with one big Bank.cpp and writing it all there, however, i don't want to make one huge file.
I assume this is a bad design problem, however, i'm still interested if it possible to implement this class as I wanted.Even if I define LoginController && Departure structure inside Bank i want separate cpp files for each class, however, header guard will close access to it after including it to one of cpp files.The best solution i can see is defining all in one header but not inside each other.
And one more question.Is it possible to define 1 header file with all includes like
#ifndef HeaderFile
#define HeaderFile
#include <iostream>
#include <memory>
....
#endif
From the previous question, i understand that this will open only for one file, therefore do we need to provide each file with needed headers. Including the same header to different files seems expensive.
You can put the #include directives inside the Bank class.
Bank.h
class Bank {
#include "LoginController.h"
#include "Departure.h"
// Bank implementation
}
LoginController.h
class LoginController {
// some implementation
}
Departure.h
class Departure {
// some implementation
}
Related
NOTE: Reupload of a question wrongly marked as duplicate
I'm working with Ogre, but my question resides specifically within namespaces.
I haven't been able to find an answer that helps me here.
I'm trying to forward declare Ogre::xyz classes within my header file for a CameraController.
This is the header file
class Ogre;
class Ogre::SceneNode;
class Ogre::SceneManager;
class CameraController
{
private:
Ogre::SceneNode* camNode;
Ogre::SceneManager* scnMgr;
};
This is the cpp file
#include "CameraController.h"
#include <OgreSceneManager.h>
#include <OgreSceneNode.h>
... definitions of functions.
What's the correct way to achieve what I'm trying to do here, in avoiding including unneeded header files within the CameraController.h file
ATTEMPT TO FIX
I attempted the redefinition as marked in a 'duplicate' that talked about declaring classes in namespaces:
namespace Ogre
{
class SceneManager;
class SceneNode;
class Camera;
class Viewport;
class Real;
}
class CameraController
{
private:
Ogre::Real getAspectRatio();
private:
Ogre::SceneNode* camNode;
Ogre::Camera* camera;
Ogre::Viewport* viewPort;
Ogre::SceneManager* scnMgr;
};
EDIT
So the error I am having now is that the classes that I forward declare within the Ogre namespace are being redefined by the headers that I include in the .cpp file
The capitalization of ViewPort and Viewport in the forward declaration is different. C++ would see them as different values.
Apologies if you have seen this question before however it has yet to be answered, essentially in my code I have two structs, defined in separate headers and used globally throughout the project. I simply wish to use both structs (which again, are defined in two separate headers) in other cpp files than just the ones that the header file belongs to.
Here is some sample code which I have tested:
class1.h
#include "class2.h"
#include <vector>
#include <string>
struct trans1{
string name;
};
class class1 {
private:
vector <trans2> t2;
public:
class1();
};
class2.h
#include "class1.h"
#include <vector>
#include <string>
struct trans2{
string type;
};
class class2{
private:
vector <trans1> t1;
public:
class2();
};
errorlog:
In file included from class1.h:3:0,
from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
vector <trans1> t1;
^
class2.h:21:19: error: template argument 1 is invalid
vector <trans1> t1;
^
class2.h:21:19: error: template argument 2 is invalid
I understand that this is ridiculous code in a real world application however this is the simplest way I could demonstrate.
It is worth noting that if I simply comment out the declaration of vector t1 or t2 under 'private:' the code compiles without fail. It is just the fact I am using a second struct.
Any help anyone? Thanks.
Simply forward-declare the classes that will be used. Put all implementation code into a cpp file, not inline in the header.
Make the vector private. This way no file that includes the header can force code generation against an incomplete class.
you can try to forward declare trans1 in class2.h and trans2 in class1.h like this:
class2.h :
// includes
struct trans1;
// rest of your code
the same thing (but with trans2) in class1.h
Don't forget to add Include guards in your code!
edit: and yes, you need to change your vectors to store pointers, otherwise it won't link
You need to put the "trans" structs in their own header file(s) and include them in your class header files.
You could forward declare them, but this would require changing your vector to use pointers. (In that case I would recommend std::vector<std::unique_ptr<trans>>). This could be appropriate if your structs are big and complex.
The main advantage of the forward-declaration approach is to reduce compile times. However if the structs are really so simple as in your example, I wouldn't bother with the extra overhead of using pointers here.
If You were to do this in single .cpp file, the solution would be trivial:
struct trans1 { ... };
struct trans2 { ... };
class class1 { ... };
class class2 { .... };
Now you just need to rearrange the code to get this result in every translation unit. (the order of classes/structs in the file is important)
I can't solve this circular dependency problem; always getting this error:
"invalid use of incomplete type struct GemsGame"
I don't know why the compiler doesn't know the declaration of GemsGame even if I included gemsgame.h
Both classes depend on each other (GemsGame store a vector of GemElements, and GemElements need to access this same vector)
Here is partial code of GEMELEMENT.H:
#ifndef GEMELEMENT_H_INCLUDED
#define GEMELEMENT_H_INCLUDED
#include "GemsGame.h"
class GemsGame;
class GemElement {
private:
GemsGame* _gemsGame;
public:
GemElement{
_gemsGame = application.getCurrentGame();
_gemsGame->getGemsVector();
}
};
#endif // GEMELEMENT_H_INCLUDED
...and of GEMSGAME.H:
#ifndef GEMSGAME_H_INCLUDED
#define GEMSGAME_H_INCLUDED
#include "GemElement.h"
class GemsGame {
private:
vector< vector<GemElement*> > _gemsVector;
public:
GemsGame() {
...
}
vector< vector<GemElement*> > getGemsVector() {
return _gemsVector;
}
}
#endif // GEMSGAME_H_INCLUDED
Remove the #include directives, you already have the classes forward declared.
If your class A needs, in its definition, to know something about the particulars of class B, then you need to include class B's header. If class A only needs to know that class B exists, such as when class A only holds a pointer to class B instances, then it's enough to forward-declare, and in that case an #include is not needed.
If you deference the pointer and the function is inline you will need the full type. If you create a cpp file for the implementation you can avoid the circular dependecy (since neither of the class will need to include each others .h in their headers)
Something like this:
your header:
#ifndef GEMELEMENT_H_INCLUDED
#define GEMELEMENT_H_INCLUDED
class GemsGame;
class GemElement {
private:
GemsGame* _gemsGame;
public:
GemElement();
};
#endif // GEMELEMENT_H_INCLUDED
your cpp:
#include "GenGame.h"
GenElement::GenElement()
{
_gemsGame = application.getCurrentGame();
_gemsGame->getGemsVector();
}
Two ways out:
Keep the dependent classes in the same H-file
Turn dependency into abstract interfaces: GemElement implementing IGemElement and expecting for IGemsGame, and GemsGame implementing IGemsGame and containing a vector of IGemElement pointers.
Look at the top answer of this topic: When can I use a forward declaration?
He really explains everything you need to know about forward declarations and what you can and cannot do with classes that you forward declare.
It looks like you are using a forward declaration of a class and then trying to declare it as a member of a different class. This fails because using a forward declaration makes it an incomplete type.
I know how to define a class method inside of the same file with which the class is in.
For example:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
void Robot::Moves()
{//Method definition here }
I do NOT know hot to define a class outside of the file in which the class is in. I tried creating a .hpp file and defining a class method inside of it, but my compiler signified that it could not load a class definition from a file other than the one the class was created in, or it would be as though I was placing a function's definition before the include directives.
Please note: The original class file is also in a .hpp file, as I have yet to learn how to use .cpp files other than the main one.
This is done using C++/Win32.
Create a .cpp file along these guidelines
Include your class header file in your .cpp file
Include all necessary headers you would use in main.cpp
use the scope operator for your class
#include <iostream>
#include "foo.hpp"
foo::foo()
{
// code here
}
void foo::OtherFunc()
{
// other stuff here
}
Just put your definition in a .cpp file and link it with your application.
Robot.hpp:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
Robot.cpp:
#include "Robot.hpp"
void Robot::Moves()
{//Method definition here }
I'm gonna go C++ über n00b on this one and ask how is the best way to deal with a circular dependency when you have inheritance.
The set is simple: Scene class extends Actor; Scene has a pointer to a vector of Actors; Actor has a pointer for (parent) Scene.
As for include files I got:
Scene.h:
#include <string>
#include <vector>
using namespace std;
#ifndef __Scene_h__
#define __Scene_h__
#include "Actor.h"
namespace myns
{
// class Actor;
class Scene;
}
namespace myns
{
class Scene: public myns::Actor
{
/* private class attributes... */
public:
/* public class attributes... */
std::vector<myns::Actor*> actors;
Scene(/* arguments */);
/* public class methods... */
};
}
#endif
Actor.h
#include <string>
#include <vector>
using namespace std;
#ifndef __Actor_h__
#define __Actor_h__
#include "Scene.h"
namespace myns
{
// class Scene;
class Actor;
}
namespace myns
{
class Actor
{
/* private class attributes... */
public:
/* public class attributes... */
myns::Scene* scene;
Actor();
Actor(/* arguments */);
/* public class methods... */
};
}
#endif
But this gives me alot of C2504 errors/base class undefined on Visual Studio 2010.
If I comment the Scene.h include on the Actor.h and uncomment the forward declaration of Scene on Actor.h it works, but then, in my app, if I want to include only the Actor.h on a particular piece of code, it will not work. How can I put this to work while maintaining the inclusion independence for Actor.h - including Actor.h without the need of previously manually including Scene.h?
What is wrong with my class definitions and how is the best way to deal with this circular dependency?
Shouldn't the #ifndef directives prevent this inclusion problem?
Thanks in advance.
but then, in my app, if I want to include only the Actor.h on a particular piece of code, it will not work
What you need to do is in the .cpp file where you need to use define the Actor class you must include both Actor.h and Scene.h. That way the forward declaration will be resolved and everything should work.
As an aside, you should move your #ifndef and #define right to the top of the file, before the includes. Also, having a using in a header file is bad practice because other files that include your header might not work properly. It should be ok to put it inside your namespace myns { ... } though.
Is a Scene really a type of Actor?
If it is, Actors probably shouldn't know about Scenes. Base classes shouldn't usually know about their derived classes.
Where is the Liskov Substitution Principle here? What action do you perform on an Actor that would be polymorphically performed differently by a Scene.
In any case, Scene derives from Actor so must include its base class. But in Actor.h if you really do need the Scene class it must be a forward declaration only.
In the compilation units (the .cpp files) you might include both headers if required.