简体   繁体   English

如何制作指向动态分配对象的指针向量?

[英]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: 您的代码有几处错误:

  • You shouldn't pass your vector<Martian*> as an argument to addMartian , instead you can just access it through the this pointer. 您不应将vector<Martian*>作为参数传递给addMartian ,而只能通过this指针进行访问。
  • There is no need for your int& iterator , as all you are trying to do is add your Martian to the end of a vector 不需要int& iterator ,因为您要做的就是将Martian添加到向量的末尾
  • Your code 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.

相关问题 将非动态分配的对象推送到指针向量 - Pushing non dynamically allocated objects to a vector of pointers 如何制作 function 指针的 std::vector? - How can I make a std::vector of function pointers? C ++清除指向动态分配对象的指针向量导致异常? - C++ Clearing vector of pointers to dynamically-allocated object causes exception? 将动态分配的对象添加到对象指针数组 - Adding dynamically allocated objects to array of object pointers 在删除动态分配对象的指针向量中的元素之前,我需要做什么? - What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects? 如何释放动态分配的向量? - How to free dynamically allocated vector? 如何检查指向动态分配的单指针的双指针是否为NULL? - How can I check whether double pointer that point dynamically allocated single pointer is NULL? 如何在向量中存储指向静态分配对象的指针? - How to store pointers to statically allocated objects in a vector? 在C ++中使用指向动态分配对象的指针向量时,如何避免内存泄漏? - How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++? 指向动态分配对象的指针向量:go 会出现什么错误? - Vector of pointers to dynamically-allocated objects: what could go wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM