3 dimensional string vectors

I am trying to write a text rpg game. I'm going in the of multi-dimenional vectors to load the Monster data. I have a text file I will be reading from. I thought before I brought it in through a class and populate the vector I'd better figure out how 3 dim vectors work... No good. Do I have to bring it in through a class? No direct elemental access? In the code you'll see the methods I tried to access the vector, and the error messages with each method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;


int main ( int argc, char *argv[] ) {

vector < vector < vector <string> > > Monster;
string name="Orc";
string plural="Orcs";
string desc="The orc is a smallish humanoid with piglike facial features.";

//Monster[0][0][0].push_back("Orc");// invalid conversion from 'const char*' to 'char'
//Monster[0][0][0].push_back(name); // No matching function for call to std::basic string <char> push_back(string&)
//Monster[0][0][0].assign("Orc");   // crashes at runtime  Exception Code:	c0000005
//Monster[0][0][0].assign(name);    // crashes at runtime  Exception Code:	c0000005
//Monster[0][0][0] = "Orc";         // crashes at runtime  Exception Code:	c0000005
//Monster[0][0][0] = name;          // crashes at runtime  Exception Code:	c0000005

return 0;
}


I looked up Exception code C0000005. This is what I got.

Exception code c0000005 is the code for an access violation. That means that your program is accessing (either reading or writing) a memory address to which it does not have rights. Most commonly this is caused by:

Accessing a stale pointer. That is accessing memory that has already been deallocated. Note that such stale pointer accesses do not always result in access violations. Only if the memory manager has returned the memory to the system do you get an access violation.
Reading off the end of an array. This is when you have an array of length N and you access elements with index >=N.
To solve the problem you'll need to do some debugging. If you are not in a position to get the fault to occur under your debugger on your development machine you should get a crash dump file and load it into your debugger. This will allow you to see where in the code the problem occurred and hopefully lead you to the solution. You'll need to have the debugging symbols associated with the executable in order to see meaningful stack traces.

Okay Gurus... make me fell stupid :) What am I doing wrong?
Monster → vector < vector < vector <string> > >
Monster[0] → vector < vector <string> >
Monster[0][0] → vector <string>
Monster[0][0][0] → string

Lines 16 and 17 illegal as you cannot push string into string. you can only push characters in string

Lines 18-21 leads to carash as there is no element 0 in any vector: you did not create those elements yet and operator[] can only index existing elements.

One way to make it work:
1
2
3
Monster.emplace_back(); //Creating one element for outer vector
Monster[0].emplace_back(); //Creating element for middle vector
Monster[0][0].emplace_back("Orc"); //Inserting string into middle vector 
Topic archived. No new replies allowed.