Vector not printing

Hi. So im using a vector to create a contain, so that when the player is asked in PlayerChoise() if they want to keep an item, the item has somewhere to go.
However when writing the PrintBag function I keep getting the same error, iv followed tutorials and searched online but I cant see what I am doing wrong.

So my Vector is there to take the top item of stack Chest and store it. I then want to print the contents at somepoint so that the player knows what is in their RuckSack.
Error on line:
cout << "Bag Item: " << NewRucksack[i].Name() << endl;


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <vector>
#define LootNumber 14
using namespace std;

// the 3 arrays stand for the 3 stats of each item
string NameOption[] = { "Stone", "Leather Gloves", "Dragon Gauntlets", "Chair Leg", "Dragon Scale Helmet", "Pebble", "Rusted Breastplate", "Dragon Breastplate", "Empty Bottle", "Chainmail Trousers", "Dragon Skin Trousers", "Broken Stick", "Dagger", "Dragons Sword" };
int RarityOption[] = { 0, 1, 3, 0, 3, 0, 2, 3, 0, 2, 3, 1, 2, 3 };
bool SetOption[] = { 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 };


class Loot		// grouping Name Rarity and Set
{
public:
	string Name;
	int Rarity;
	bool Set;

	Loot(string N, int R, bool S) // constructor initialises each of the fields
	{
		Name = N;
		Rarity = R;
		Set = S;

	}

};


class TreasureChest
{
public:
	stack<Loot> Chest;

	// defines default constructor
	TreasureChest(string E)
	{
		E = "Empty";
	}

	TreasureChest() // for loop gives the same random item every time return too later
	{
		int i = rand() % LootNumber;
		int j = rand() % LootNumber;
		int k = rand() % LootNumber;
		int h = rand() % LootNumber;

		Chest.push(Loot(NameOption[j], RarityOption[j], SetOption[j]));
		Chest.push(Loot(NameOption[k], RarityOption[k], SetOption[k]));
		Chest.push(Loot(NameOption[i], RarityOption[i], SetOption[i]));
		Chest.push(Loot(NameOption[h], RarityOption[h], SetOption[h]));
	}

};


//Gives player choise whether to keep or discard each loot item as it is revealed from the stack
void PlayerChoice()
{
	PlayerRucksack();
	char Answer;
	cout << "If you want to keep the item press Y" << endl;
	cout << "If you want to discard the item press N" << endl;
	cin >> Answer;
	while (Answer == 'Y' || Answer == 'y')
	{
		cout << "Item stored in your bag" << endl;
		FillBag(PlayerRucksack().Rucksack);
		// store item in bag array using pointer
		return;
	}
	while (Answer == 'N' || Answer == 'n')
	{
		cout << "Item was discared from the Treasure Chest" << endl;
		return;
	}
	while (Answer != 'y' || Answer != 'Y' || Answer != 'N' || Answer != 'n')
	{
		cout << "To decide press Y for accept OR press N for Decline" << endl;
		cin >> Answer;
		if (Answer == 'Y' || Answer == 'y') {
			cout << "Item stored in your bag" << endl;
			//store item in bag
			return;
		}
		else (Answer == 'N' || Answer == 'n'); {
			cout << "Item was discared from the Treasure Chest" << endl;
			return;
		}
		return;
	}
}


// function that prints the contents of TreasureChest to the screen whilst asking the player if they want each item.
void PrintTreasureChest()
{

	TreasureChest A;
	int j;
	for (j = 1; j < 5; j++)
	{
		cout << "Item " << j << " in your chest is: " << endl;
		cout << "Name:" << (A.Chest.top()).Name << " Rarity out of 3: " << (A.Chest.top()).Rarity << " Part of a set: " << (A.Chest.top()).Set << endl;
		PlayerChoice();
		A.Chest.pop();
	}
	cout << "This chest is now empty" << endl;

	return;
}

class PlayerRucksack
{
public:
	vector<Loot> Rucksack;
};

void FillBag(vector<Loot> &NewRucksack)
{
	Loot NewLoot(NameOption[0], RarityOption[0], SetOption[0]);
	NewRucksack.push_back(NewLoot);
}

void PrintBag(const vector <Loot>&NewRucksack)
{
	for (unsigned int i = 0; i < NewRucksack.size(); i++)
	{
		cout << "Bag Item: " << NewRucksack[i].Name() << endl;

	}
}

int main()
{
	PrintBag(Rucksack);
	return 0;
}
Last edited on
> I keep getting the same error
> Error on line:
> cout << "Bag Item: " << NewRucksack[i].Name() << endl;
¿what error?
full message, verbatim.


In function ‘void PlayerChoice()’:
|65 col 17| error: ‘PlayerRucksack’ was not declared in this scope
|73 col 36| error: ‘FillBag’ was not declared in this scope
|91 col 3| warning: this ‘else’ clause does not guard... [-Wmisleading-indentation]
In function ‘void PrintBag(const std::vector<Loot>&)’:
|134 col 47| error: no match for call to ‘(const string {aka const std::__cxx11::basic_string<char>}) ()’
|141 col 11| error: ‘Rucksack’ was not declared in this scope


`NewRucksack' is a `vector'
`NewRucksack[i]' is a `Loot'
`Loot::Name' is a `std::string'
`string' has no () operator.


about the warning
else (Answer == 'N' || Answer == 'n'); {
I suppose that you put the semicolon `;' to silent an error
else does not need a clause.
Last edited on
Topic archived. No new replies allowed.