I'm trying to template the return type for this function (component), I've looked around for example code but there doesn't seem to be any exactly like what I want, could someone help me out? thanks.
template<typename T>
T& Entity::component<T>()
{
for (unsignedint i = 0; i < m_Components.size(); ++i)
{
if (m_Components.at(i).name == T.name)
{
return m_Components.at(i);
}
}
return *(new Component); //will fix this later
}
main.cpp
1 2 3 4 5
EntityManager EntManager;
Entity ent1 = EntManager.createEntity();
ent1.addComponent("HealthComponent"); //will make it templated later
ent1.component<HealthComponent>.health = 10;
error
'ent1.component<HealthComponent>' does not have class type
'ent1.component<HealthComponent>' does not have class type
After seeing that you look at the code and see that component is, indeed, not a class type, so you look more closely at the offending line and the declaration component.
"Hmm.. it's a function," you say to yourself. "Shouldn't I be using parentheses to indicate a function call?"