类对象指针的2D向量C ++

时间:2018-04-02 03:02:45

标签: c++ class pointers vector 2d

我试图创建类对象指针的动态2D向量。我试图为基于文本的游戏制作随机地图。我知道那里有解决方案,但我想亲手制作这只小狗。我只是......吮吸指针。

我尝试创建一个类指针的2D数组,但语法很难遵循。我真的不知道从哪里开始。我上一个C ++课程是在一年前,我们只是简单地进入了向量。

我试图自己做一些研究,但我似乎无法将这些概念融合在一起。我已经能够参考以下帖子/页面:

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/

现在,我正在制定计划的第一步。我知道,一旦我将2D矢量语法缩小,其余的将落实到位。但是,这不是我之前做过的事情。我确信我的编码对大多数人来说都不太适合,但它已经有一段时间......

如果我可以要求澄清以下内容,我认为这将对我有很大的帮助。

1。如何通过引用将2D向量传递给函数?如何正确操作指针内的指针?

2。如何在2D矢量中访问指针的类成员函数?

第3。如何动态创建2D向量中指针指向的类对象?

#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;
}

1 个答案:

答案 0 :(得分:0)

这是一个简短的代码示例。

// 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;
}

首先想到的是,如果可能,您应尽量避免使用new。您应该考虑使用std::unique_ptrstd::make_unique而不是向量中的原始指针 它基本上是一个指针,它也拥有它指向的对象,所以当指针被销​​毁时你不需要手动delete它。

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);
相关问题