Accessing a vector from another class

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]);
	}
}
Try putting this after the class enemyBoss definition in enemyBoss.h (which you labelled as enemyBoss.cpp above):

 
inventoryContainer enemyBoss::inventoryCon;

BTW, stop posting twice!

EDIT: Now that I think about it, the code above should be put in the actual enemyBoss.cpp file, just after the includes.
Last edited on
snip hold up a second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <Windows.h>
#include <vector>
#include<string>
#include<iostream>
using namespace std;

static class inventoryContainer {
public:
	 vector<string> subContainer{};
	static vector<string>mainInventory;

	void AddItemToSub(int index) {
		subContainer.push_back(mainInventory[index]);
	}

}InventoryContainer;

vector<string> inventoryContainer::mainInventory{ "axe","sword","bat" };

class enemyBoss
{

private:
	
	int health = 100;
	

public:
	vector<string> inventory{};
	//enemyBoss();
	// ~enemyBoss();

	void addToInventory() {
		this->inventory = InventoryContainer.subContainer;
	}
};

int main() {

	enemyBoss eb{};
	InventoryContainer.AddItemToSub(0);
	InventoryContainer.AddItemToSub(2);
	eb.addToInventory();

	for (auto m : eb.inventory) {
		std::cout << m << '\n';
	}
}


ya thats kinda annoying. Geez. I'm rusty. Edited again sry.
Last edited on
Thank you for the answers, I will try this.

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