Sign in to follow this  
UselessRob

Help with the order of defining classes?

Recommended Posts

UselessRob    122
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
TangentZ    450
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

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