Currently I have 3 classes, set up like this:
World.h
include "WorldObject.h"
class WorldObject;
class TextObject; // when this is added, compiles fine, but tobj is incomplete when accessed
//(world->tobj->function()). if #include "TextObject.h" is added, numerous errors occur within TextObject.h
class World {
public:
WorldObject* wobj; // works fine
TextObject* tobj; //trying to get this to be functional
};
WorldObject.h
#include "World.h"
class World;
class WorldObject {
public:
WorldObject(World* world){...} // works fine, world can be accessed from world objects
};
TextObject.h
#include "WorldObject.h"
#include "World.h"
class TextObject : WorldObject {
public:
TextObject(World* world) : WorldObject(w){...};
};
How can I use forward declaration so that tobj will be accessible from World.h, as obj is, with no errors? I am also using #pragma once at the beginning of each class. I have attempted to add "class World" to TextObject.h and "class TextObject" to World.h, but none of the seemingly standard procedures are working. Any suggestions?
#include <WorldObject.h> in your .cpp file rather than your header and you can access it from there.
Your header includes are causing circular dependencies, which you avoid through forward declarations.
Related
I have the following code, there is an 'Face' class which is composed of an 'Eyebrow' class. The expression of the face can be changed to one of the allowed public enums, thus controlling how each of it's constituent should be changed. Each of the constituent classes should be responsible for responding to a changed expression which is why I want to pass that along to each constituent.
face.h:
#include "eyebrow.h"
class Face {
public:
enum Expression {NEUTRAL, JOY, ANGER};
void setExpression(Expression);
private:
Eyebrow left_eyebrow;
}
face.cpp:
#include "face.h"
#include "eyebrow.h"
void Face::setExpression(Expression e) {
left_eyebrow.setExpression(e);
}
eyebrow.h:
#include "face.h"
class Eyebrow {
public:
void setExpression(Face::Expression);
};
The code is being compiled with Cmake:
add_executable(Main main.cpp face.cpp eyebrow.cpp)
I am getting the following compiler error: ‘Face’ has not been declared in void setExpression(Face::Expression_e) in eyebrow.h.
I then forward declared Face (see below) and got the compiler error: ‘Face::Expression’ has not been declared in void setExpression(Face::Expression); in eyebrow.h
eyebrow.h (with forward declaration):
#include "face.h"
class Face;
class Eyebrow {
public:
void setExpression(Face::Expression);
};
What is the recommended way of solving this, should friends be used?
You have created a circular dependency:
face.h includes eyebrow.h and eyebrow.h includes face.h, so they're including each other. You must resolve the circular dependency.
You could remove the enum from Face and put it in its own header file, which both files could then include.
Alternatively, you could store a pointer to Eyebrow in Face instead of Eyebrow itself. Then you could remove the include of eyebrow.h in face.h and forward-declare Eyebrow instead. The pointer can then be initialized in the constructor of Face.
In below code while compiling i am getting error ,
Error :
main.cpp:8:57: error: expected type-specifier before ‘Staircase’
std::unique_ptr algodiagnostic (new Staircase());
compilation command
g++ HardwareDiagnostic.cpp HardwareDiagnostic.h main.cpp -std=c++0x -o res
it works fine if compile entire code in single file without creating header file and main.cpp separately. Can anybody suggest how to fix this issue.
//HardwareDiagnostic.h//
#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_
#include <iostream>
#include <memory>
using namespace std;
class HardwareDiagnostic
{
public:
virtual bool checkPort();
virtual void startDiagnostic();
virtual int publishAlgoDiagnosticInfo();
virtual void clearErrorStatus(){cout<<"Algorithm Diagnostics"<<endl;}
virtual ~HardwareDiagnostic() {cout<<"calling virtual destructor"<<endl;}
};
#endif /* HARDWAREDIAGNOSTIC_H_ */
//HardwareDiagnostic.cpp//
#include"HardwareDiagnostic.h"
class Localization : public HardwareDiagnostic
{
public:
Localization() { cout << "calling Localization constructor";}
bool checkPort(){cout<<"checkport :Localization";}
void startDiagnostic(){cout<<"start Diagnostic:Localization";}
int publishAlgoDiagnosticInfo() {cout<<"publish Diagnostic:Localization";}
void clearErrorStatus(){ cout<<"Localization:publish Diagnostic";}
~Localization () { cout<<"calling Localization destructor ";}
};
class Staircase : public HardwareDiagnostic
{
public:
Staircase () {cout<<"Staircase constructor";}
bool checkPort(){cout<<"Staircase";}
void startDiagnostic(){cout<<"StairCase:start Diagnostic";}
int publishAlgoDiagnosticInfo() {cout<<"StairCase:publish Diagnostic";}
void clearErrorStatus(){ cout<<"staircase:publish Diagnostic";}
~Staircase(){cout<<"calling Staircase destructor";}
};
//main.cpp//
#include "HardwareDiagnostic.h"
using namespace std;
int main() {
std::unique_ptr<HardwareDiagnostic> algodiagnostic (new Staircase());
return 0;
}
When compiler works on main.cpp, it sees Staricase usage, but doesn't see declaration for Staircase class, so error is raised. Your main.cpp file includes header, which describes HardwareDiagnostic class only, but no info for Staircase is provided.
Good practice is to keep class declarations in header files, rather than cpp files, so any other source can include header file and start using described class. It's OK to include definition for trivial class methods in header file as well (like getters/setters), but complicated methods should be declared in header file and defined in coresponding cpp file.
In your case I would do the following:
HardwareDiagnostic.h describes HardwareDiagnostic class. There is no using std line in this file, so anyone including this file won't implicitly start using undesired namespace. All methods are declared, but not defined.
HardwareDiagnostic.cpp defines methods of HardwareDiagnostic class. So there is using std line and definition of all methods.
Localization.h includes HardwareDiagnostic.h and describes Localization class. Having separate file for each class becomes very convenient as project grows.
Localization.cpp defines methods of Localization class.
Staircase.h and Staircase.cpp are built the same way as localization.
main.cpp includes HardwareDiagnostic.h and Staircase.h
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'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.
I checked out a post similar to this but the linkage was different the issue was never resolved. The problem with mine is that for some reason the linker is expecting there to be a definition for the base class, but the base class is just a interface. Below is the error in it's entirety
c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxdx.h(2) : error C2504: 'GXRenderer' : base class undefined
Below is the code that shows how the headers link with one another
GXRenderManager.h
#ifndef GXRM
#define GXRM
#include <windows.h>
#include "GXRenderer.h"
#include "GXDX.h"
#include "GXGL.h"
enum GXDEVICE {
DIRECTX,
OPENGL
};
class GXRenderManager {
public:
static int Ignite(GXDEVICE);
private:
static GXRenderer *renderDevice;
};
#endif
at the top of GxRenderManager, there is GXRenderer , windows, GXDX, GXGL headers. I am assuming by including them all in this document. they all link to one another as if they were all in the same document. correct me if I am wrong cause that's how a view headers. Moving on...
GXRenderer.h
class GXRenderer {
public:
virtual void Render() = 0;
virtual void StartUp() = 0;
};
GXGL.h
class GXGL: public GXRenderer {
public:
void Render();
void StartUp();
};
GXDX.h
class GXDX: public GXRenderer {
public:
void Render();
void StartUp();
};
GXGL.cpp and GXDX.cpp respectively
#include "GXGL.h"
void GXGL::Render()
{
}
void GXGL::StartUp()
{
}
//...Next document
#include "GXDX.h"
void GXDX::Render()
{
}
void GXDX::StartUp()
{
}
Not sure whats going on. I think its how I am linking the documents, I am not sure.
The problem is You need to have #include "GXRenderer.h" at the top of both: GXGL.h and also GXDX.h.
The base type must be defined not just declared before defining a derived type.
By the way, the error is a compiling error not linking error.
Edit: About your class type redefinition:
at the top of every header file you should have #pragma once.
The #pragma once directive specifies that the file will be included at most once by the compiler in a build.
You included them all into GXRenderManager.h, meaning that GXRenderManager.h is OK.
But you forgot to include them all into GXGL.cpp and GXDX.cpp. In these .cpp files GXRenderer class is completely unknown.
There are at least two "schools" of #include strategies. One says that header file must include everything that is needed for its own compilation. That would mean that GXGL.h and GXDX.h must include GXRenderer.h. If you followed that strategy, your GXGL.cpp and GXDX.cpp would be OK as they are now.
Another "school" says that header files must not include each other at all, i.e. all inclusions must be done through .cpp files. At first sight one could guess that your GXGL.h and GXDX.h follow that strategy (since you are not including anything into them), but then your GXRenderManager.h looks completely different.
You need to decide which strategy you are trying to follow and follow it. I'd recommend the first one.
I got an error C2504: 'CView' : base class undefined
where CView is not directly my base class from which I am inheriting.
I am inherting mYClass from MScrollView, "for this matter any class which is not actual Base Class is what the point is to be noted down here"
but the error is the C2504. When I have included it in the header where this problem is arising, this problem is resolved.
#include "stdafx.h"
where stdafx.h has #include which contains all the basic class defined...hope this answer resolves everyone who are facing this issue.