Templated Class Function, does not have a class type

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.

Entity.hpp

1
2
3
4
5
6
7
8
9
10
11
12
class Entity
{
public:
    Entity();
    unsigned int id = 0;
    Component& addComponent(std::string);
    bool removeComponent(std::string);
    bool hasComponent(std::string);
    template<typename T> T& component();
private:
    std::vector<Component> m_Components;
};


Entity.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename T>
T& Entity::component<T>()
{
    for (unsigned int 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
Last edited on
'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?"
holy crap, I can't believe I didn't see that, thanks :]
Topic archived. No new replies allowed.