• Advertisement
Sign in to follow this  

Help with the order of defining classes?

This topic is 4978 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

Howdy I've got a problem. And I would think it's common but I can't find the solution. It's probably an amateur, newb, beat me in an alley with a stick if you ever find me, type question, but I'm going to be brave and ask it anyway. I have 2 classes in a program that have functions that take each other as input. But how do I define that so it works? I'll try and make it make sense. I have CLASSA and it has a function EatWeasels(CLASSB *b) like that. and CLASSB has a function LeprechaunSoup(CLASSA *a). The problem is the compiler complains because when CLASSA is defined CLASSB hasn't yet been defined. There must be a way around it tho? There just has to be. And it's probably simple. And I'm probably just stupid. But I definately need help! Thanks! Rob.

Share this post


Link to post
Share on other sites
Advertisement
The answer is forward declaration.


// In A.h
class B; // Forward declaration

class A
{
public:
void EatWeasels(B *b);
}
// end

// In A.cpp
#include "A.h"
#include "B.h"

void A::EatWeasels(B *b) { /* blah */ }

// In B.h
class A; // Forward declaration

class B
{
public:
void LeprechaunSoup(A *a);
}
// end

// In B.cpp
#include "B.h"
#include "A.h"

void B::LeprechaunSoup(A *a) { /* blah */ }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement