I'm attempting to create a dynamic 2D vector of class object pointers. I'm trying to make a randomized map for a text based game. I know there's solutions out there, but I want to hand build this puppy. I just.. suck at pointers.
I tried to create a 2D array of class pointers, but the syntax was quite difficult to follow. I don't really know where to begin. My last C++ class was a year ago and we only briefly went into vectors.
I've attempted to do some research on my own, but I just can't seem to blend the concepts together. I've been able to reference the following posts/pages:
Vector of Object Pointers, general help and confusion
http://www.cplusplus.com/doc/tutorial/pointers/
vector of class pointers initialization
https://www.geeksforgeeks.org/2d-vector-in-cpp-with-user-defined-size/
Right now, I'm on step one of my plan. I know once I get the 2D vector syntax down, the rest will fall into place. However, it's just not something I've ever done before. I'm sure my coding is not quite up to snuff for most, but it's been awhile...
If I may ask for clarification on the following, I think it would be a huge help to me.
1. How do you pass a 2D vector to a function by reference and what would the syntax be to manipulate the pointers held within properly?
2. How do you access class member functions of pointers within a 2D vector?
3. How do you dynamically create class objects that are pointed to by pointers in a 2D vector?
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
using namespace std;
enum Dir{n, e, s, w};
class Object
{
private:
string objName;
public:
void setName(string n)
{
objName=n;
}
string getName() const
{
return objName;
}
Object()
{
objName="";
}
Object(string n)
{
objName=n;
}
virtual ~Object()
{
cout << "The " << objName << " was destroyed.\n";
}
};
class Room : public Object
{
private:
Room *north, *east, *south, *west;
public:
void setDir(Room *d, Dir a)
{
switch(a)
{
case n: north=d; break;
case e: east=d; break;
case s: south=d; break;
case w: west=d; break;
}
}
Room *getDir(Dir a)
{
switch(a)
{
case n: return north; break;
case e: return east; break;
case s: return south; break;
case w: return west; break;
}
}
Room(){}
Room(string rName, Room *n, Room *e, Room *s, Room *w) : Object(rName)
{
north=n;
east=e;
south=s;
west=w;
}
};
Room Wall;
void RoomRandomizer(vector<Room *> map, string rName)
{
int x=0, y=0, entX=0, extY=0;
bool entFound = false;
Room * tempRoom;
string rN = rName;
srand(time(NULL));
if(rName == "Entrance")
{
x=rand() % 7+1;
y=rand() % 5;
tempRoom = new Room(rName, &Wall, &Wall, &Wall, &Wall);
map[x][y]= tempRoom;
}
};
int main(){
int row=9, colom[]={9,9,9,9,9,9,9,9,9};
Wall.setName("Wall");
vector<vector<Room *>> map(row);
for (int i = 0; i < row; i++) {
// size of column
int col;
col = colom[i];
// declare the i-th row to size of column
map[i] = vector<Room *>(col);
//map.resize(9, vector<Room *>(9, 0));
}
map[0][0] = new Room("Entrance", Wall, Wall, Wall, Wall);
cout << map[0][0]->getName;
return 0;
}