在各种函数中使用push_back更新类对象的向量

发布于 2024-12-19 02:35:54 字数 948 浏览 1 评论 0原文

我有一个通过读取数据文件在 main 中创建的类对象向量。然后,我将向量传递到几个不同的文件,其中包含对向量执行不同操作的函数(按不同字段排序、减去库存等)。当我尝试使用 push_back 添加到另一个文件(同一项目的一部分)中的矢量创建后,我遇到了问题。预先存在的向量被传递给函数,并且向量被成功添加到函数内,但是当我退出函数时,添加的记录不再存在,并且据我所知,我应该访问更新的记录从不同文件中的所有不同函数向前的向量,对吗?如果更新后的向量与我在 main 中创建的向量同名,我就不必将其传回,不是吗?我确信我使用的语言和术语是错误的(请随时纠正我),但这几乎就像矢量没有全局更新,而只是在调用期间在函数内进行本地更新。

这是我要添加到向量的函数中的函数定义和代码(为了简洁起见,我跳过了所有变量参数分配)。我指向的所有函数都是类中的 setter 函数,在此之前向量已经包含 20 条记录,并且设置与以下函数类似。

void addBook(vector<BookData> books)
{
    BookData *books1;
    books1 = new BookData;
    books1->setTitle(bookTitle);
    books1->setAuthor(author);
    books1->setPub(publisher);
    books1->setIsbn(isbn);
    books1->setWholesale(wholesale);
    books1->setRetail(retail);
    books1->setQty(qtyOnHand);
    books1->setDateAdded(dateAdded);
    books.push_back(*books1);
}

我不想在帖子中充斥太多代码,但如果有帮助的话我可以发布更多代码。我只是希望在整个程序中可以访问添加(或在另一个函数中进行的删除)。

I have a vector of class objects I've created in main by reading in a data file. I'm then passing around the vector to several different files containing functions that perform different operations on the vector (sorting by different fields, subtracting inventory, etc.). I'm running into a problem when I try to use push_back to add to the vector in another file (that's a part of the same project) after it's already been created. The pre-existing vector is passed to the function and the vector is successfully added to within the function, but when I exit the function the added record is no longer there, and, as far as I can tell, I should be accessing the updated vector after that point forward from all my different functions in different files, right? I shouldn't have to pass the updated vector back if it's the same name as the one I created in main, should I? I'm sure the language and terminology I'm using are wrong (and please feel free to correct me), but it's almost as if the vector isn't updating globally and is only updating locally within the function for the duration of its call.

This is the function definition and code within the function I'm using where I want to add to the vector (I'm skipping all the variable parameter assignments for the sake of brevity). All of the functions I'm pointing to are the setter functions within the class, and prior to this point the vector already contains 20 records, and was set up similarly to the below function.

void addBook(vector<BookData> books)
{
    BookData *books1;
    books1 = new BookData;
    books1->setTitle(bookTitle);
    books1->setAuthor(author);
    books1->setPub(publisher);
    books1->setIsbn(isbn);
    books1->setWholesale(wholesale);
    books1->setRetail(retail);
    books1->setQty(qtyOnHand);
    books1->setDateAdded(dateAdded);
    books.push_back(*books1);
}

I didn't want to flood the post with too much code but I can post more if it'd be helpful. I just want the additions (or deletions I make in another function) to be accessible throughout the whole program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-12-26 02:35:54

您应该通过引用传递向量来修改原始全局向量。

void addBook(vector<BookData>& books)
                            ^^^

否则,您将原始向量的副本传递给函数并修改它而不是全局版本。

You should pass the vector by reference to modify the original global vector.

void addBook(vector<BookData>& books)
                            ^^^

Otherwise you are passing a copy of the original vector to the function and modifying that not the global version.

蓝礼 2024-12-26 02:35:54

您需要在必要时传递矢量作为参考。在该特定实例中,您只需更改

void addBook(vector<BookData> books)

为:

void addBook(vector<BookData>& books)

否则您的函数将获取向量的副本,而不是对原始向量的引用。

You need to pass your vector as a reference wherever necessary. In that specific instance, you just need to change

void addBook(vector<BookData> books)

to:

void addBook(vector<BookData>& books)

otherwise your function gets a copy of the vector, not a reference to the original one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文