Updating vector of class objects using push_back in various functions

后端 未结 2 870
余生分开走
余生分开走 2021-01-20 03:50

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 p

相关标签:
2条回答
  • 2021-01-20 04:09

    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 讨论(0)
  • 2021-01-20 04:21

    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.

    0 讨论(0)
提交回复
热议问题