How to make one class recognize a class object (located beneath it)

closed account (N8RzwA7f)
So I have this;

class Item {
...
}
class Inventory {
...
}

class Produce : public Item {
...
}
class Book : public Item {
...
}

is there a way to make the Inventory class recognize Produce and Book objects ?
I have tried forward declarations, composition etc nothing works.
Thanks.
closed account (N8RzwA7f)
But I already tried that and it didn't work. I declared them right at the top ?
So in order for the inventory class to be recognized by the Produce and Book classes, you need to put Inventory below Produce and Book. Have you tried doing that?

Also, share some actual code you wrote.
Hi,

Each of your class declarations should be in it's own header file (.hpp) and the implementations should be in their own .cpp files all of which are named exactly after the class name. For example Item.hpp and Item.cpp Then, #include the header file for the class you want to use, just watch out for circular includes.

Good Luck !!
closed account (N8RzwA7f)
Thanks, now it's working, I assumed that wouldn't work.
I assumed that wouldn't work.

I'm curious - what made you think it wouldn't work?

Do you now understand why it does work?
Topic archived. No new replies allowed.