-2

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;
}
RLee
  • 106
  • 9
  • 1. Add a `&`. 2. With A `->`. 3. In this case with `new`, but look up `std::make_unique`. Unrelated: I also recommend looking up what `#include ` does. the way you use it here has me worried you're wandering into cargo cult programming. – user4581301 Apr 02 '18 at 05:11

1 Answers1

0

Here is a short code examples.

// Passing in the vector of vectors by reference
void function(std::vector<std::vector<Room*>>& referece_to_2d_vector) {

    // Call member function
    Room* north = referece_to_2d_vector[0][0]->getDir(n);

    // Just like you already do in main, dynamically allocating a new
    // room and make one of the pointers point to it
    referece_to_2d_vector[0][0] = new Room("New room", &Wall, &Wall, &Wall, &Wall);

    // Is this what you mean when you say "manipulate the pointers held within" ?
    referece_to_2d_vector[0][1] = north;
}

The first thing that comes to mind is that you should try to avoid using new if possible. You should look into maybe using std::unique_ptr and std::make_unique instead of raw pointers in the vector.
It is basically a pointer that also owns the object it points to, so when the pointer is destroyed you don't need to manually delete it.

std::vector<std::vector<std::unique_ptr<Room>>> map(1);

map[0].push_back( std::make_unique<Room>("Room1", &Wall, &Wall, &Wall, &Wall) );
map[0].push_back( std::make_unique<Room>("Room2", &Wall, &Wall, &Wall, &Wall) );

// You can get a raw pointer to the object owned by a `std::unique_ptr` with it's `get` function.
map[0][0]->setDir( map[0][1].get(), n);
super
  • 12,335
  • 2
  • 19
  • 29
  • Thanks for the help, Super! And yes, that was what I was referring to when I said "manipulate the pointers held within"; I wanted to see how to point the pointers inside the object to other objects and point the pointers in that object back. – RLee Apr 02 '18 at 14:41