forward declaration for cyclic includes..

Started by
2 comments, last by MTclip 18 years, 5 months ago
hey, I have a cyclic include thing going on, it should be fine because i am forward declaring.. whats goin on here

// perception.h
#pragma once

#include "agent.h"
// forward Declaration
class Agent;

class Perception
{
public:
	Agent* pClosestKin;
	Agent* pClosestMate;
	Agent* pClosestFood;
	Agent* pCloasestThreat;

	Perception(void);
	~Perception(void);
};


//agent.h
#pragma once
//other includes clipped//
#include "perception.h"

#define MUTATION 15

// forward declaration
class Perception;

class Agent : public WereIRenderable, public WereIFileable
{
protected:
	static std::map<std::string, Agent*> m_AgentMap;
	
public:
	static long* pAgentID;
	std::string m_location;

	WereModel * m_pModel;
	Behavior* m_pBehavior;
	Stats* m_pStats;
	AgentState m_state;
	Perception m_perception;
// rest of class clipped//

have i forward declared wrong? both of the classes compile if i dont try to mix them here is the error Agent.h(36): error C2079: 'Agent::m_perception' uses undefined class 'Perception' thanks
Advertisement
try


#ifndef SOME_NAME
#define SOME_NAME

// your code

#endif


might be worth a try
Your forward declaraions are fine, but perception.h doesn't need to include agent.h because it only uses Agent as a pointer. Move that #include to perception.cpp, and the circular dependancy will go away.

Also, don't rely on #pragma once to prevent a header from being included more than once, because it's a compiler-specific extension. It's best to stick with the old fashioned method of include guards:

#ifndef INCLUDED_PERCEPTION_H_#define INCLUDED_PERCEPTION_H_/* the header */#endif // INCLUDED_PERCEPTION_H_
do that instead of using #pragma once?

edit:: I GOT IT!! thanks fellas

This topic is closed to new replies.

Advertisement