Hello, I am trying to make a little console based game, and i need a vector that holds inventory items that end boss' can hold. I do not want this vector (v1)to be stored in the end boss class, as I want this vector (v1) to simply hold every item that is possible. And then each end boss character will have their own inventory vector (v2), and at the start of the game, certain items from v1 will be pushed into v2. So I need a way to access v1, from the end boss class.
As I said I did not want the v1 to be created in the end boss class as I figured that would be a waste of memory (each end boss object holding this vector), so i created v1, in its own class, and then created it as an object in the end boss class. I wanted to create it as a static vector, so that it was shared between all the enemy boss objects, as I believe it would be a waste of memory for all the end boss objects to hold this vector themselves, but when I have it as static, i get an error. Without static it works fine. But I would like to try and make it as a real world program would be made. So, is it possible to make it static, or is there a better way to do this vector? Is this how this inventory vector would be represented in a real world program?
Vector (v1) container class
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <vector>
using std::string;
using std::vector;
class inventoryContainer
{
private:
public:
vector <string> inventoryItems = { "Sword", "Axe", "Mallet", "Hammer" };
};
enemyBoss cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
#include "enemy.h"
#include "inventoryContainer.h"
class enemyBoss : public enemy
{
private:
int health = 100;
vector <string> inventory;
static inventoryContainer inventoryCon;
public:
enemyBoss();
~enemyBoss();
void addToInventory();
};
enemyBoss header
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "enemyBoss.h"
#include "inventoryContainer.h"
void enemyBoss::addToInventory()
{
for (int i = 0; i < 3; i++)
{
i = rand() % 3 + 1;
inventory.push_back(inventoryCon.inventoryItems[i]);
}
}
I am not sure why it posted twice, many times when i try and post I get an error message so i need to try post again, so that is might be why. I never noticed it had posted twice or I would have deleted the second.
It's fine. People here are a little trigger happy because you will get other users that spam a question in multiple subforums, but a glitch in the forum itself is less avoidable. (It's clear it was a glitch because both of your threads were posted at the same time in the same subforum.)