I get an error when I try to cast base class to derived class. I want to access the derived classes that I have put in my components vector.
//Base and Derived
class Component
{
public:
    Component();
    virtual ~Component();
private:
};
class Material:public Component{...};
//in main
int textureID = gameScene.gameObjects[0].getComponent<Material>()->texture;
//game object
#pragma once
#include <vector>
#include "Component.h"
class GameObject:public Component
{
public:
    GameObject();
    GameObject(int otherAssetID);
    ~GameObject();
    int assetID;
    std::vector<Component> components;
    void addComponent(Component otherComponent);
    void deleteComponent(Component otherComponent);
    template <class T>
    T* getComponent() {
        for (int i = 0; i < components.size(); i++)
        {
            if (dynamic_cast<T*>(components[i]) != nullptr)
            {
                T *test = dynamic_cast<T*>(components[i]);
                return test;
            }
        }
        return nullptr;
    }
private:
};
 
     
    