A fruit needs to be aware of the branch on which it grows. Moreover, I am supposed to write a function for all branches that would decrease the weights of all produce on them. Because of that, I thought of using forward declarations for classes, yet the code refuses to work, specifically the fd function. I have no idea why. What causes the problem?
It's because you're trying to dereference a fruit before the compiler knows the full definition of the FRUIT_CLASS.
In your Branch class, just declare unsignedint fd();
Then after the FRUIT_CLASS is defined, implement the fd function outside of the class definition itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class FRUIT_CLASS;
class BRANCH_CLASS {
// ...
unsignedint fd();
};
class FRUIT_CLASS {
// ...
};
unsignedint BRANCH_CLASS::fd()
{
// ...
}
Your other issue is that you aren't returning anything from your fd() function.
Third issue is that your dereferencing logic is wrong. Pointer arithmetic already takes into account the sizeof the class (which one reason why the full definition needs to be known).
Just use array syntax. It's the same thing.fruit[some_index]->fadeFruit();
PS: Having (void) to mean no parameters is not needed in C++. You can just do ().