Hi,
im currently developing a scenegraph for 3d graphics and need to do double dispatch which require me to pass one class as argument for another class that passes itself to the first class, which means that class 1 includes class 2 and class 2 includes class 1. In c++ this isnt really allowed as they tend to include each other over and over again causing endless loop :P
How do i solve this? (on top of this i got 10 more classes that sometimes needs access to either class 1 or class 2 (or both))
Code for class 1 (node.h):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef NODE_H
#define NODE_H
#include <cstdlib>
#include "state.h"
#include "nodevisitor.h"
class Node{
public:
Node();
~Node(){}
//Add some functions to accept visitors here
virtualvoid accept(NodeVisitor* visitor);
void setState(State *st){state = st;}
State* getState(){return state;}
private:
State* state;
};
#endif
code for class2 (nodevisitor.h)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef NODEVISITOR_H
#define NODEVISITOR_H
#include "group.h" //<-- These 3 classes all inherit from class Node at top
#include "transform.h"
#include "geometry.h"
class NodeVisitor{
public:
NodeVisitor();
~NodeVisitor();
void traverseNodes();
virtualvoid apply(Group* group);
virtualvoid apply(Transform* transform);
virtualvoid apply(Geometry* geometry);
};
#endif
#ifndef NODE_H
#define NODE_H
#include <cstdlib>
#include "state.h"
/* #include "nodevisitor.h"
Change this into the following line */
class NodeVisitor; //which creates a "temporary link" to the actual class
class Node{
public:
Node();
~Node(){}
//Add some functions to accept visitors here
virtualvoid accept(NodeVisitor* visitor);
void setState(State *st){state = st;}
State* getState(){return state;}
private:
State* state;
};
#endif
EDIT: Hmm it didnt solve it at all, it just solves it for when you need references, but since my local NodeVisitor class doesnt have anything defined it still doesnt work since i call functions that are not defined yet...so i still need help! ^^
EDIT2: Found out this phenomena was called "circular dependencies" :P (that's what you get for being primarily java programmer lol...) So im gonna scout around abit for that, but i'm still glad for any help on this matter.
EDIT3: Ok now it is finally solved! What i did was do forward declarations in the header files like above, and instead of including the headers i need in the header file, i just included them in the actual implementation instead (the .cpp file) :P