Declaring vectors for class objects within another class

Hi there

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
Last edited on
Create a vector in 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).

#include that header file
So it is possible to include a header file in another header file?
Yes. Just make sure you use correct code guards or #pragma once if supported to protect against multiple inclusions.

You can also forward declare a class so that it can be referenced (in some circumstances or as a pointer) before it is defined.
Oh, that's cool! That solves so many issues I am having with my code.

#pragma once is supported and I'm already using it since I included some of the header files in multiple .cpp files.

Thanks! :-)
#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
Last edited on
Topic archived. No new replies allowed.