使用push_back在各种函数中更新类对象的向量 [英] Updating vector of class objects using push_back in various functions

查看:144
本文介绍了使用push_back在各种函数中更新类对象的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

你应该传递一个函数通过引用修改原始全局向量的向量。

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.

这篇关于使用push_back在各种函数中更新类对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆