Some cyclic references problem
CObjects.h
Code:
#include "NodePool.h"
class NodePool;
class CObjects
{
class PathPoint
{
PathPoint(const NodePool::Node& node) { }
}
};
NodePool.h
Code:
#include "CObjects.h"
class CObjects;
class NodePool
{
class Transition
{
float getCost(CObjects* unit)
{
...
}
};
};
I try to comment out either includes, but there are some reference problems.
Can't post the errors, they aren't in English. How do I break the ties?
Thanks
Jack
Re: Some cyclic references problem
In NodePool.h you could use only the forward declaration for CObjects and put the implementation of the function
Code:
float getCost(CObjects* unit)
in a .cpp file with the #include for CObjects.h in the .cpp file.
For header files it is best to use a forward declaration when possible and it is possible if you have only an object reference or a pointer that is not dereferenced in the header.
Re: Some cyclic references problem
Thanks Yadrif, I'll try what you've suggested
Re: Some cyclic references problem
Thanks, your solution works.
Re: Some cyclic references problem
I come across another cyclic problem
Code:
#ifndef TIMEASTAR_H
#define TIMEASTAR_H
#include <set>
#include <bitset>
#include <assert.h>
#include <vector>
class CObjects;
class SNodePool;
#define USE_BITSET true
class TimeAStar
{
class Node;
public:
class Transition
{
public:
virtual Node* FromNode() = 0;
virtual Node* ToNode() = 0;
virtual float getCost(CObjects* unit) = 0;
};
class Node
{
public:
Node() { x = 0; z = 0; t = 0; id = 0; f = 0; g = 0; h = 0; }
Node(int _x, int _z, int _id) : x(_x), z(_z), id(_id) { f = 0; g = 0; h = 0; t = 0; }
~Node() { }
int id;
float f;
float g;
float h;
int depth;
Transition* transition;
int x;
int z;
int t;
int getID() { static int d = 0; return d++; }
virtual std::vector<Transition*>& getTransitions() { std::vector<Transition*>dummy; return dummy; }
virtual float getActualTimelessCost(const TimeAStar::Node &dest)
{
return (abs(x - dest.x) + abs(z - dest.z));
}
};
public:
class Path
{
public:
//std::vector<Transition*> transitions;
std::vector<SNodePool::SNode> path;
Code:
#ifndef NODEPOOL_H
#define NODEPOOL_H
#include <sstream>
#include <map>
#include <vector>
//#include "CrowdEntity.h"
//#include "TimeAStar.h"
#include "Grid.h"
class TimeAStar;
class SNodePool;
class CObjects;
//class Grid;
#define infinite_cost -1.0f
using namespace std;
class SNodePool
{
// use TimeAStar in a map
This time, both "declarations" are located in the header file. I can't move them to implementation and use forward declaration. Any ideas?
Thanks
Jack
Re: Some cyclic references problem
Quote:
Originally Posted by lucky6969b
This time, both "declarations" are located in the header file. I can't move them to implementation and use forward declaration. Any ideas?
A forward declaration may suffice to define SNodePool as having a map member for which the key or mapped type is TimeAStar. I am not sure if this is guaranteed though.
Re: Some cyclic references problem
Hello laserlight,
Thanks for your help. Actually, I just use forward declarations in both file. You see that I've actually commented out the #include's in both files. VS2010 complains and underlines the line std::vector<SNodePool::SNode> path and Everything i declared with TimeAStar under the SNodePool class. Pretty hairy.
Thank you once again
Jack
Re: Some cyclic references problem
The point is, if a forward declaration suffices for one of them, you can then include the header containing that class definition into the header containing the other class definition.
Re: Some cyclic references problem
Code:
#include "NodePool.h"
class TimeAStar
{
};
Code:
class TimeAStar;
class SNodePool
{
};
I've got C2027
This should work, shouldn't it
very Wield...
I thought that might be a design flaw, each class is defining a variable of the other's class.
thanks
class
Re: Some cyclic references problem
Quote:
Originally Posted by lucky6969b
I've got C2027
This should work, shouldn't it
Yes, but that is obvious because the two classes are unrelated. If you got error C2027, then that is a compiler bug so blatant as to be impossible.
Look, if you want to post code, post the real thing, or at least a reduced version of the real thing that actually demonstrates the error.
Re: Some cyclic references problem
I've rewritten it a bit, and be able to narrow down to 7 errors C2027
Pathpoint.h
Code:
#ifndef PATHPOINT_H
#define PATHPOINT_H
#include "SNode.h"
class SNodePool;
class SNode;
class PathPoint
{
public:
int x;
int z;
long t;
PathPoint(const SNode& node) : x(node.x), z(node.z), t(node.t) <<<<<<<<<<<<<<<<< C2027 SNode
{
}
PathPoint(int x, int z, long t)
{
this->x = x;
this->z = z;
this->t = t;
}
bool isSamePlace(const PathPoint& other)
{
return (this->x == other.x) && (this->z == other.z);
}
};
#endif
SNode.h
Code:
#ifndef SNODE_H
#define SNODE_H
#include "NodePool.h"
#include "Grid.h"
#include "CrowdEntity.h"
class STransition;
class SNodePool;
class CObjects;
class SNode;
extern Grid *grid;
class SNode
{
public:
int x;
int z;
int t;
std::vector<STransition*> transitions;
SNodePool *nodepool;
public:
SNode() { }
SNode(SNodePool* p) { nodepool = p; }
SNode(int x, int z, int t, SNodePool *p) : nodepool(p) { this->x = x; this->z = z; this->t = t; }
~SNode() { }
void init(int x, int y, int t) { this->x = x; this->z = z; this->t = t; transitions.clear(); }
std::vector<STransition*>& getTransitions();
// base Not pure virtual
//float getCost (SNode *from, SNode *to)
//{
//}
float getActualTimelessCost(const SNode& dest);
};
class STransition
{
public:
SNode *fromNode;
SNode *toNode;
SNodePool *nodepool;
bool wait;
public:
STransition(SNodePool *nodepool, SNode *fromNode, SNode *toNode)
{
this->nodepool = nodepool;
this->fromNode = fromNode;
this->toNode = toNode;
this->wait = (fromNode->x == toNode->x) && (fromNode->z == toNode->z);
}
public:
SNode *FromNode() {
return fromNode;
}
SNode *ToNode() {
return toNode;
}
float getCost(CObjects* unit);
};
class SPoint
{
public:
int x;
int z;
SPoint(int x, int z)
{
this->x = x;
this->z = z;
}
bool operator==(const SPoint& other)
{
return (this->x==other.x) && (this->z == other.z);
}
};
#endif
Any help is greatly appreciated!
Thanks
Jack
Re: Some cyclic references problem
Re: Some cyclic references problem
In the PathPoint header, it is presently not sufficient to forward-declare SNode because your constructor cannot verify that SNode has x, z, and t members with only a forward declaration.
The simplest approach would be just put this in the header:
Code:
PathPoint(const SNode& node);
and move this into PathPoint.cpp (which includes SNode.h):
Code:
PathPoint::PathPoint(const SNode& node) : x(node.x), z(node.z), t(node.t)
{}