[英]How can i make a vector of pointers that point to dynamically allocated object?
I want to create a vector of pointers that each point to their own Martian object but i can't figure out how to arrange it. 我想创建一个指针向量,每个指针都指向自己的火星对象,但我不知道如何安排它。 I'm currently getting the error
我目前遇到错误
Non-const lvalue reference to type 'vector' cannot bind to a value of unrelated type 'martianDatabase'
对类型'vector'的非常量左值引用不能绑定到不相关的类型'martianDatabase'的值
but the error constantly changes with every change i make. 但是错误会随着我的每一次更改而不断变化。 I've watched a ton of tutorials the last two days trying to figure this out but I'm still stuck.
最近两天我看了很多教程,试图弄清楚这一点,但是我仍然很困惑。
struct Martian
{
Martian(string fname, string lname, string ssid);
string fname, lname, ssid;
~Martian();
};
class martianDatabase
{
vector<Martian*> database;
martianDatabase();
void addMartian(vector <Martian*> &database, int &iterator, string f, string l, string id);
int iterator = 0;
};
Martian::Martian(string f, string l, string id)
{
fname = f;
lname = l;
ssid = id;
}
void martianDatabase::addMartian(vector <Martian*> &database, int &i, string f, string l, string id)
{
Martian* m = new Martian(f, l, id);
database[i].push_back(m);
i++;
}
vector<Martian*> database;
Your database
is a std::vector
of pointers to Martian
objects. 您的
database
是指向Martian
对象的指针的std::vector
。 That's what this declaration states. 这就是声明的内容。
database[i].push_back(m);
Since database
is a vector, database[i]
would be the i
th value in this vector. 由于
database
是一个向量,所以database[i]
将是此向量中的第i
个值。 Since this vector is a vector of Martian *
, therefore database[i]
is some Martian *
value in this vector. 由于此向量是
Martian *
的向量,因此database[i]
是此向量中的某个Martian *
值。
Obviously, you understand that if you have Martian *
, a pointer to a Martian, it's not a class, and you can't push_back
() anything on it. 显然,您了解如果您拥有
Martian *
(指向Martian *
的指针),则它不是类,并且您不能在其上push_back
()任何东西。 It's a pointer. 这是一个指针。 A plain, garden variety pointer.
朴素的花园品种指针。 You can't
push_back()
anything to a pointer. 您不能将
push_back()
指向任何指针。 You can't begin()
or end()
it, etc... 您不能
begin()
或end()
,等等。
That's what the compiler is telling you, with it's error message. 这就是编译器告诉您的错误消息。
And, as far as your "How can i make a vector of pointers" question, you already did it: 而且,至于您的“我如何制作指针向量”问题,您已经做到了:
vector<Martian *> database;
That's a vector of pointers. 那是指针的向量。 Now, whether it's a vector of pointers to dynamically-allocated objects, or not, that's no longer relevant.
现在,无论它是否是指向动态分配对象的指针向量,都不再相关。 The vector doesn't care where the objects it's pointing to come from.
向量不在乎它指向的对象来自哪里。 A pointer to a dynamically-allocated object is exactly the same as a pointer to some object in a
static
scope. 指向动态分配对象的指针与指向
static
作用域中某个对象的指针完全相同。
There are several things wrong with your code: 您的代码有几处错误:
vector<Martian*>
as an argument to addMartian
, instead you can just access it through the this
pointer. vector<Martian*>
作为参数传递给addMartian
,而只能通过this
指针进行访问。 int& iterator
, as all you are trying to do is add your Martian
to the end of a vector int& iterator
,因为您要做的就是将Martian
添加到向量的末尾 database[i].push_back(m);
database[i].push_back(m);
gets the i'th element of the vector database
, which is a Martian&
, and then trys to call push_back(m)
on it, which is not possible, as there is no such function declared for type Martian
, what you probably wanted is database.push_back(m)
, which insert m
at the back of the database
vector database
的第i个元素,即Martian&
,然后尝试在其上调用push_back(m)
,这是不可能的,因为没有为Martian
类型声明此函数,您可能想要的是database.push_back(m)
,它在database
向量的后面插入m
Consider the following alternative: 考虑以下替代方法:
class martianDatabase
{
vector<Martian*> database;
martianDatabase();
void addMartian(string f, string l, string id);
};
void martianDatabase::addMartian(string f, string l, string id)
{
this->database.push_back(new Martian(f, l, id));
}
Though not realy a problem, it is potentially better to directly initialise member in your constructor (where possible) rather then copy assign them, ie use the code: 尽管这不是真正的问题,但最好直接在构造函数中初始化成员(如果可能),而不是复制分配它们,即使用代码:
Martian::Martian(string f, string l, string id) : fname(f), lname(l), ssid(id) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.