# Class Circular Dependency and Arrays of Classes

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

## 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)
/*
*/

class TFlock;

class TCreature
{
TFlock* flock;
}

/*
*/

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 == 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 on other sites
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 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 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 on other sites
Quote:
 Original post by JingoDon'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 on other sites
Quote:
Original post by LifeOfTamir
Quote:
 Original post by JingoDon'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 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 on other sites
ahhh..i see...much clearer now...thanks guys :D