I'm encountering the following problem in my code:
1. I have two classes (A & B )/>/>, each having one function ( X() & Y() ).
2. Now, I need to enter function Y(), from X(). => Error.
#include <iostream>
usingnamespace std;
//Tried but failed
class A;
class B;
class A {
public:
B B;
void x() {
//How to call?
if(B.y()) {
cout << "Function Y returned 'true'" << endl;
}
}
};
class B {
public:
bool y() {
if(5 < 2) {
returntrue;
}
elsereturnfalse;
}
};
int main() {
A A;
A.x();
return 0;
}
Errors:
Code Description Line
C2079 'A::B' uses undefined class 'B' 10
C2228 left of '.y' must have class/struct/union 12
I tried forwarding, but that ain't working very well.
Other's are talking about creating a new header file, but I personally guess that's a bit of an overkill for this problem.
class Foo {
public:
bool y();
}; // definition of Foo does not require Bar
class Bar {
Foo foo;
public:
bool x() { return foo.y(); }
};
int main() {
Bar boo;
boo.x();
return 0;
}
// implementation of Foo::y does have to know the definition of Bar
bool Foo::y() {
return 4 < sizeof(Bar);
}