Std::vector from another class - Push back desont add anything


#1

Hi - Another issue - i cant even think of key words to search google to try find solution

m_GSelected is a GGroup* class which has a std::vector<GBody*> m_GBodies;

Function

std::vector<GBody*> getBodies() { return m_GBodies; };

m_GSelected->getGAgents().push_back(body);

Why does this not add anything to the vector. I use the debugging tool in Visual Studio, to walk through it and everything is fine and nothing gets added. Vector stays empty size 0

I am using work around like so

std::vector<GBody*> m_PlaceholderBodies = m_GSelected->getBodies();
m_PlaceholderBodies.push_back(body);
m_GSelected->setBodies(m_PlaceholderBodies );

This works

Thanks for any pointers that you may have to this issue :slight_smile:


#2

std::vector<GBody*> getBodies() { return m_GBodies; }; Returns a copy of m_GBodies. You need to return a pointer or reference. Try: std::vector<GBody*>& getBodies() { return m_GBodies; };

You may want to look into references in C++.


#3

I am a noob ! Yes this makes perfect sense now… :slight_smile:
I need watch some videos on pointers and references as it still doesn’t sit well in my soul

Ill try this later