Sign in to follow this  

Class Circular Dependency and Arrays of Classes

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have two classes TCreature and TFlock. TCreature needs TFlock and TFlock has functions that take in a pointer to a TCreature array, which means that it has to know about it. This is how i had set it out (more or less i don't have the code with me i'm kinda making this up as i go along)
/*
*TCreature Header File*
*/

class TFlock;

class TCreature
{
   TFlock* flock;
}


/*
*TFlock Header File*
*/

class TCreature;

class TFlock
{
   int ID;

//function
   bool CanSeeFlockmate(TCreature* targets, int otherID);
}

And in the function CanSeeFlockmate i can't remember exactly the code so i'll make something up that will display the same error
bool TFlock::CanSeeFlockmate(TCreature* targets, int otherID)
{
   for (int i=0; i<flockSize; i++)
   {
      if (targets[i] == ID)
         return true;
   }

   return false;
}

You get the point, targets is an array and i want to access it's members. But i get these errors: error2036: 'class TCreature*' : unknown size error2027: Use of undefined type 'TCreature' see definition of 'TCreature' Does anyone know a way around this? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could always put them in the same header file.

// forward declare your flock
class CFlock;

class CCreature
{
CFlock *m_pFlock;
};


class CFlock
{
// etc...
};


- Joe

Share this post


Link to post
Share on other sites
Don't put them in the same header, you simply have to include the correct header in the .cpp file, not the .hpp file.

So if that function is in tflock.cpp, you simply add


#include"tcreature.hpp"


Into tflock.cpp.

Share this post


Link to post
Share on other sites
In flock.cpp, are you including creature.cpp? (and vice verse) It is right what you are doing by just declaring the classes in the header files, but in the cpp file, it is ok to include the other header.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jingo
Don't put them in the same header, you simply have to include the correct header in the .cpp file, not the .hpp file.

So if that function is in tflock.cpp, you simply add


#include"tcreature.hpp"


Into tflock.cpp.


In the .cpp files i'm including the header file
so

//in creature.cpp
#include 'creature.h'

//in flock.cpp
#include 'flock.h'

And since the .h files include the class declareation doesn't that mean i don't have to include creature.cpp in flock.cpp?

@wendigo23 that kinda answers your question too

Share this post


Link to post
Share on other sites
Quote:
Original post by LifeOfTamir
Quote:
Original post by Jingo
Don't put them in the same header, you simply have to include the correct header in the .cpp file, not the .hpp file.

So if that function is in tflock.cpp, you simply add


#include"tcreature.hpp"


Into tflock.cpp.


In the .cpp files i'm including the header file
so

//in creature.cpp
#include 'creature.h'

//in flock.cpp
#include 'flock.h'

And since the .h files include the class declareation doesn't that mean i don't have to include creature.cpp in flock.cpp?


No, a declaration is enough to declare/define a pointer to a class type, but to use the pointer in an array expression, a definition of the class is required(the compiler needs to know the size of the class). So you must include creature.h in flock.cpp.

Share this post


Link to post
Share on other sites
In your TFlock Header File #include the TCreature Header File. You need to have the full definition of the class, not just the declaration in order to use it in the manner that you are inside of the CanSeeFlockmate member function.

Share this post


Link to post
Share on other sites

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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