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.
我正在尝试创建类对象指针的动态2D向量。我正在尝试为基于文本的游戏制作随机地图。我知道那里有解决方案,但我想亲手制作这只小狗。我只是......吮吸指针。
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.
我试图创建一个类指针的2D数组,但语法很难遵循。我真的不知道从哪里开始。我上一个C ++课程是在一年前,我们只是简单地进入了向量。
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...
现在,我正在实施我的计划的第一步。我知道,一旦我将2D矢量语法缩小,其余部分就会落实到位。然而,这不是我以前做过的事情。我敢肯定我的编码对大多数人来说都不是很好,但已经有一段时间......
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?
1.如何通过引用将2D向量传递给函数?如何正确操作指针内的指针?
2. How do you access class member functions of pointers within a 2D vector?
2.如何在2D向量中访问指针的类成员函数?
3. How do you dynamically create class objects that are pointed to by pointers in a 2D vector?
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 个解决方案
#1
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 :: unique_ptr和std :: make_unique而不是向量中的原始指针。它基本上是一个指针,它也拥有它指向的对象,因此当指针被销毁时,你不需要手动删除它。
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);
#1
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 :: unique_ptr和std :: make_unique而不是向量中的原始指针。它基本上是一个指针,它也拥有它指向的对象,因此当指针被销毁时,你不需要手动删除它。
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);