Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:50:33+00:00 2026-05-27T06:50:33+00:00

I have a vector of class objects I’ve created in main by reading in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:50:34+00:00Added an answer on May 27, 2026 at 6:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a vector of class objects. The following program crashes with Pointer
I have a vector-like class that contains an array of objects of type T
I have a class that has a vector of another class objects as a
I have a class with a vector of pointers to objects. I've introduced some
I have one 'main' thread that creates an array of objects of class SlowData
I have a vector (order is important) of objects (lets call them myobj class)
If I have a vector of objects in one class which I want to
I have a vector with some (among other classobjects) multiple added objects class Foo
I have some vectors of class A objects: std::vector<A> *V1; std::vector<A> *V2; etc there
I have created a BST full of WordInfo objects that have a vector to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.