Sign in to follow this  
MTclip

forward declaration for cyclic includes..

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try


#ifndef SOME_NAME
#define SOME_NAME

// your code

#endif


might be worth a try

Share this post


Link to post
Share on other sites
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_

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this