Why can't I add items into my vector?

peterboston

I have the following code in one class:

    class A
    {
        std::vector<Parameter*> params;
        std::vector<Parameter*> getParamVector() {return params;}
        void addOneParam(Parameter* param)
        {
            params.push_back(param);
        }
    }

In another class, class B, I try to add items into the params vector by doing this:

classA_Ptr->getParamVector().push_back(aParamPtr);

But the params vector's size still 0, after above call.

I have to add above addOneParams(Parameter* param) method to add items into the params vector. Why?

Barry

getParamVector() returns a copy of params. So when you push_back onto it, you're adding to a temporary vector that gets deleted right away. That in no way affects params's size.

If you want to be able to do this via getParamVector(), you'll have to return a reference to params instead:

std::vector<Parameter*>& getParamVector() {return params;}
                      ^^^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I display my items in Flatlist?

From Dev

Why I can't add items to the Generic List with indexer?

From Dev

Why can't I add items to an array if it is inside a singleton class?

From Dev

"there are no allowed placeholders" - why can't I add forms to my presentation?

From Dev

Why can't I add a main to my library in golang?

From Dev

Why can't I add a Form label to my Label list?

From Dev

Why can't I add a border on my svg path?

From Dev

Why is my DataGrid with a ObservableCollection<T> apparently not refreshing when I add or remove items from the collection?

From Dev

Why can't I change objects in a vector?

From Dev

Why can't I add top margin to my <a> tag when I am using bootstrap?

From Dev

Why can't I wrap a T* in an std::vector<T>?

From Dev

How can I add and change items in my Applications Menu?

From Dev

Can I add more start menu items to my program with JavaFX?

From Dev

How can I add and change items in my Applications Menu?

From Dev

How can i add glyphicons to my navbar items?

From Dev

Why can't I inject my service?

From Dev

Why can't I call my function?

From Dev

Why can't I reload my table

From Dev

Why can't I call my function?

From Dev

Why can't I delete my files?

From Dev

Why can't I update my Ubuntu?

From Dev

Why i can't return my method?

From Dev

Why can't I stop my timer

From Dev

Why can't you add items to arrays in a loop?

From Dev

Why can't I add a earlier xamarin forms version as a nuget into my Xamarin forms project/android package?

From Dev

Why can't I add an int member to my GLSL shader input/output block?

From Dev

Why can't I add new object to my array of object pointers?

From Dev

Why i can't add my tree an element without returnnig the pointer of root?

From Dev

Why can't I add the newtonsoft.Json.dll ref to my project?

Related Related

  1. 1

    Why can't I display my items in Flatlist?

  2. 2

    Why I can't add items to the Generic List with indexer?

  3. 3

    Why can't I add items to an array if it is inside a singleton class?

  4. 4

    "there are no allowed placeholders" - why can't I add forms to my presentation?

  5. 5

    Why can't I add a main to my library in golang?

  6. 6

    Why can't I add a Form label to my Label list?

  7. 7

    Why can't I add a border on my svg path?

  8. 8

    Why is my DataGrid with a ObservableCollection<T> apparently not refreshing when I add or remove items from the collection?

  9. 9

    Why can't I change objects in a vector?

  10. 10

    Why can't I add top margin to my <a> tag when I am using bootstrap?

  11. 11

    Why can't I wrap a T* in an std::vector<T>?

  12. 12

    How can I add and change items in my Applications Menu?

  13. 13

    Can I add more start menu items to my program with JavaFX?

  14. 14

    How can I add and change items in my Applications Menu?

  15. 15

    How can i add glyphicons to my navbar items?

  16. 16

    Why can't I inject my service?

  17. 17

    Why can't I call my function?

  18. 18

    Why can't I reload my table

  19. 19

    Why can't I call my function?

  20. 20

    Why can't I delete my files?

  21. 21

    Why can't I update my Ubuntu?

  22. 22

    Why i can't return my method?

  23. 23

    Why can't I stop my timer

  24. 24

    Why can't you add items to arrays in a loop?

  25. 25

    Why can't I add a earlier xamarin forms version as a nuget into my Xamarin forms project/android package?

  26. 26

    Why can't I add an int member to my GLSL shader input/output block?

  27. 27

    Why can't I add new object to my array of object pointers?

  28. 28

    Why i can't add my tree an element without returnnig the pointer of root?

  29. 29

    Why can't I add the newtonsoft.Json.dll ref to my project?

HotTag

Archive