Search code examples
c++templatescompiler-errorssyntax-errorgame-engine

ERROR: S2760: Unexpected token '<', expected ',' inside a ECS


Hello big brains of the internet :). I am making my way deeper into the depths of C++. I received this error and even tho it should be an easy one, I don't have the faintest idea where have I made mistake.

Here is a declaration of the template etc.:

using ComponentID = std::size_t;

inline ComponentID getComponentTypeID()
{
    static ComponentID lastID = 0;
    return lastID++;
}

template <typename T> inline ComponentID getComponentTypeID() noexcept
{
    static ComponentID typeID = getComponentTypeID();
    return typeID;
}

This is the code of the whole class of an Entity Component System I am currently working on:

class Entity
{
private:
    bool active = true;
    std::vector<std::unique_ptr<Component>> components;

    ComponentArray componentArray;
    ComponentBitSet componentBitSet;
public:
    void update()
    {
        for (auto& c : components) c->update();
        for (auto& c : components) c->draw();
    }
    void draw() {}

    bool isActive() const { return active; }
    void destroy() { active = false; }

    template <typename T> bool hasComponent() const
    {
        return componentBitSet[getComponentTypeID<T>()];
    }

    template <typename T, typename... TArgs>
    T& addComponent(TArgs&&... mArgs)
    {
        T* c(new T(std::forward<TArgs>(mArgs)...));
        c->entity = this;
        std::unique_ptr<Component> uPtr{ c };
        components.emplace_back(std::move(uPtr));

        ComponentArray[getComponentTypeID<T>()] = c;
        ComponentBitSet[getComponentTypeID<T>()] = true;

        c->init();
        return *c;
    }

    template<typename T> T& getComponent() const
    {
        auto ptr(componentArray[getComponentTypeID<T>()]);
        return *static_cast<T*>(ptr);
    }
};

The mistake should be somewhere along this line:

ComponentArray[getComponentTypeID<T>()] = c;

I tried checking it, rewriting it, my roomie checked it over, but my eyes decieve me... Please help?


Solution

  • I'm not sure what all Component, ComponentArray, and ComponentBitSet look like exactly, but perhaps these lines:

        ComponentArray[getComponentTypeID<T>()] = c;
        ComponentBitSet[getComponentTypeID<T>()] = true;
    

    should look like this:

        componentArray[getComponentTypeID<T>()] = c;
        componentBitSet[getComponentTypeID<T>()] = true;
    

    ComponentArray and ComponentBitSet are just types, indexing into them isn't a thing. componentArray and componentBitSet are the members of your Entity class corresponding to those types, and (I imagine) are of some type that allows indexed access.