So I’m still working on my test-RPG game and have another question.
I now have a (sort of) working inventory but I’m not really happy with it.
The inventory holds different class objects that may be created at different places within the program (mostly within functions). I’m storing these objects in vectors, using one vector for each type (class) of object. I then provide the Player with a menu from which he can choose an item category to access the corresponding vector and see all objects of that class within his inventory. So far so good.
Now comes the problem:
The inventory must be attached to the player character. So ideally the vectors holding the inventory would be part of the player class. But as far as I understand it (and the compiler seems to agree with me ;-) ), this is not possible. Because when I try to declare the vectors within the player class, the compiler rightfully complains that the data types I try to declare the vectors with are not known (as they are declared in a separate header file).
So currently, I declare the vectors in the main function which is certainly not the right way to do it. And of course this only works for one player as I have not found a way to then attach the vectors to a specific player.
So, how do I solve this? How do I declare a vector inside the player class that can hold class objects that are declared in separate header files?
My program structure currently looks like this (only the relevant stuff of course):
Player.hpp // header file for player class
Items.hpp // header file for classes of all the different items
Main.cpp // main program
Player.cpp // constructor, destructor, functions of player class
Items.cpp // constructors, destructors, functions of item classes
the compiler rightfully complains that the data types I try to declare the vectors with are not known (as they are declared in a separate header file).
#include "filename.h" means "copy the contents of filename.h right here, literally as if I had opened that file in a text editor and copy-pasted the text myself".
You can #include any file you like, anywhere you like - all it does is copy text. Whatever text is there, whatever it says, anything at all. It's up to you to ensure it makes sense. A number of conventions exist in C++ about what should and should not go into files that we call "header files" and what/where files should be #include'd