Hey all. As mentioned in the title, I'm trying to create a function that returns a list of all the objects of the specified type from another list(list as in vector). However, the way in which I have implemented it currently doesn't seem to be working.
The current implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template<typename T>
inline std::vector<T*> GameObject::GetGameObjectOfType()
{
std::vector<T*> temp;
for (GameObject* g : ObjectList)
{
if (typeid(g) == typeid(T))
{
T* t = dynamic_cast<T*>(g);
temp.push_back(t);
}
}
return temp;
}
Edit: T will always be a derived type of GameObject.
At the moment, the above line creates a runtime error because the returned vector doesn't have any elements added to it. The error I get is vector subscript out of range.
How should I go about implementing this function so that it doesn't create any errors?