views:

104

answers:

2

I had a post similar to this awhile ago based on a error I was getting. I was able to fix it but since then I been having trouble doing things because headers keep blocking other headers from using code. Honestly, these headers are confusing me and if anyone has any resources that will address these types of issues, that will be helpful.

What I essentially want to do is be able to have rModel.h be included inside RenderEngine.h. every time I add rModel.h to RenderEngine.h, rModel.h is no longer able to use RenderEngine.h. (rModel.h has a #include of RenderEngine.h as well).

So in a nutshell, RenderEngine and rModel need to use each others functionalities. On top of all this confusion, the Main.cpp needs to use RenderEngine.

stdafx.h

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "resource.h"

main.cpp

#include "stdafx.h"
#include "RenderEngine.h"
#include "rModel.h"

// Global Variables:

RenderEngine go;

rModel *g_pModel;

...code...........

rModel.h

#ifndef _MODEL_H
#define _MODEL_H
#include "stdafx.h"
#include <vector>
#include <string>
#include "rTri.h"
#include "RenderEngine.h"

........Code

RenderEngine.h

#pragma once
#include "stdafx.h"
#include "d3d10.h"
#include "d3dx10.h"
#include "dinput.h"
#include "rModel.h"

.......Code......
+5  A: 

As I wrote in my previous answer on this question, google about Forward declaration in C++. This may solve your problems, but, again, circular header dependencies indicate poor application design.

Kotti
Yea I got that the last time. I was trying to take a whole new approach to the whole situation and get a more precise answer. Where it seem the first issue was based around a particular issue or error, this was a broader approach to the whole situation and what the best practices are. but It seems this is a common matter and Forward declaration is the obvious answer to these types of situations. I guess I will further research that. thanks
numerical25
Ok, so I understand that forward declarations are just like function prototypes. They hold off until the rest of the document is finished. In this case, I would have to put both classes in the same document. But I remember you saying I could put the headers in seperate documents. How would I go about doing this ? Right now i got the forward declaration of rModel in RenderEngine. And I have a include of RenderEngine.h in rModel.h. But the main.cpp has already declared the RenderEngine.h so RenderEngine.h in rModel is being ignored.
numerical25
nevermind, I am taking several different approaches now. I removed the RenderEngine.h from the main.cpp and just included rModel.h since it has a declaration of RenderEngine.h in it. That did not fix the problem. but I think I am catching on.
numerical25
http://pastie.org/976205
Kotti
Note that this sample actually makes an infinite loop by calling methods from each other. Of course, you should avoid this, but, well, just for you to know it's possible.
Kotti
I actually am trying to implement this forward declaration, But I think I am not doing it right. Not sure. I did read up on it. when i google it, alot comes up about function forward declaration and a few on class. Here is my code I got now. I really don't want to keep creating new post on this http://pastie.org/976249
numerical25
There is one sh*tty place in your code, that actually spoils everything. It's your nested class declaration. It is OK, but if you're trying to use such a nested (public!) class somewhere else, your compilation fails. There are obvious technical reasons for that. Anyway, you could use such a nested class in some of your functions declared in a `.cpp` file, but not in your `.h` file.
Kotti
So, to sum up, take that public nested class out of your `RenderEngine`. If you still want to have your logical division like `Vertex` is a part of your rendering part, you could make a namespace and call it `render` and add all your classes to it.
Kotti
To make your code compilable, you should pull those nested classes out of your `RenderEngine` and change all references to it appropriately.
Kotti
ok, thanks alot
numerical25
+3  A: 

At least if I understand your question correctly, you have a little bit of a problem. You basically need to structure your headers so the inclusions form a directed acyclic graph (emphasis on acyclic).

What you may have to do is break your "renderengine.h" into two pieces, one of which just contains forward declarations, and the other of which contains the rest of your current contents. You'll then include that "forward declarations" header into "rmodel.h", and include "rmodel.h" into "renderengine.h".

While there are times that this is unavoidable, such a circular dependency often points to a problem with how you've organized your modules. It's entirely possible that what you currently have as renderengine.h and rmodule.h should be a single header, or perhaps multiple headers but broken along different lines.

Jerry Coffin