Help with the order of defining classes?
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.
The answer is forward declaration.
// In A.hclass B; // Forward declarationclass A { public: void EatWeasels(B *b); }// end// In A.cpp#include "A.h"#include "B.h"void A::EatWeasels(B *b) { /* blah */ }// In B.hclass A; // Forward declarationclass B { public: void LeprechaunSoup(A *a); }// end// In B.cpp#include "B.h"#include "A.h"void B::LeprechaunSoup(A *a) { /* blah */ }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement