0

Hey All,

I've been stumbling into the same (annoying little) problem a couple of times, and I'd like to find out how to fix it properly.

Pseudo code

// file A.hpp
#include "classB.hpp"
class A { A() };


// file A.cpp
A::A() { }
// file B.hpp
#include "classA.hpp"
class B { B() };


// file B.cpp
B::B() { }

Notice how they're basically two normal classes, except that the both include the other.
I do have #ifndef CLASS_A / CLASS_B in the headers (header gaurds).

If i do #include "classA.hpp" in file classB.cpp I get errors like so:
error: ‘A’ was not declared in this scope

If I do a dummy class class B; then I can't use any of the functions in that class, because it complains that the class doesn't have that functionality.

Help apprecicatied, as I'm not sure how to design for such a situation... :)
-Harry

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mazzica1
0

i hope this could be helpful

#include <iostream>
using namespace std;

class A;

class B{
	A *obj;
public :
	B(){obj=0;}
	~B(){if(obj)delete obj;}
	void helloFromA();
};
class A{
public :
	void hello()
	{
		cout<<"hello from A"<<endl;
	}
};


void B::helloFromA()
{
	if(!obj)
	{
		obj=new A();;
	}
	obj->hello();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.