Why am I getting error C2061 (syntax error: identifier)?

18,459

What is the point of the declaration class Component; if you include Component.h, anyway? You cannot inherited from an incomplete type, so this declaration makes no sense.

A wild guess is circular include issues, but it's really hard to tell without more information. I recommend reading this article on Organizing Code Files in C and C++ if you are unclear on the subject.

Edit:

Player.addComponent(StaticSprite, new StaticSprite());

You cannot pass the type StaticSprite to a method. That's probably the source of the error. Show us the declaration of addComponent. What is the first parameter type?

Edit:

void Entity::addComponent(TYPE type, void* component)

Okay, so what is the definition of TYPE? I guess it's just a typedef for int or something? In that case it should be clear that you cannot assign the type StaticSprite to an int.

Edit:

enum TYPE{Base = 0, StaticSprite, DynamicSprite, 
     Physics, Collision, Input, Particle, Audio, Scriptable, MaxType};

Yep, there you have it. The enumerator TYPE::StaticSprite has absolutely nothing to do with the type StaticSprite, even though they have the same name. Give the enumerator some other name, for example T_StaticSprite, and the following code should work:

Player.addComponent(T_StaticSprite, new StaticSprite());

(Whether this whole type enumeration thing is a good idea is a different question.)

Share:
18,459
epicasian
Author by

epicasian

Updated on June 04, 2022

Comments

  • epicasian
    epicasian almost 2 years

    Okay... I've been wrestling this error for at least an hour now. I have my StaticSprite class inherit from my Component class. However, when I try to instantiate a StaticSprite instance, I get error c2016.

    Here's my relevant code, I'll post more if need be. Thanks in advance.

    StaticSprite.h

    #ifndef STATICSPRITE_H_
    #define STATICSPRITE_H_
    
    #include "Component.h"
    #include <SFML/Graphics.hpp>
    #include <string>
    
    namespace GE
    {
     class Entity;
     class Component;
     class StaticSprite : public Component
     {
     private:
      sf::Image image;
     public:
      sf::Sprite Sprite;
      StaticSprite();
      StaticSprite(std::string filename);
     };
    }
    
    #endif
    

    Component.h

    #ifndef COMPONENT_H_
    #define COMPONENT_H_
    
    #include "Entity.h"
    #include "ComponentType.h"
    #include <iostream>
    
    namespace GE
    {
     class Entity;
     class Component
     {
     protected:
      Entity* myEntity;
      TYPE type;
     public:
      Component();
      virtual ~Component() {}
      Entity* getEntity();
     };
    }
    
    #endif
    

    main.cpp

    int main(int argc, char* argv[])
    {
        Entity Player;
        //The first arg is just a number, and the second is the 
        //actual component to add to the Entity's component array
        Player.addComponent(StaticSprite, new StaticSprite()); //error
    
        //application loop
    }
    

    Entity.cpp

    //TYPE is just an enumerated type
    void Entity::addComponent(TYPE type, void* component)
    {
    Components[type] = component;
    }
    

    ComponentType.h

    #ifndef COMPONENTTYPE_H_
    #define COMPONENTTYPE_H_
    
    namespace GE
    {
    enum TYPE{Base = 0, StaticSprite, DynamicSprite, 
        Physics, Collision, Input, Particle, Audio, Scriptable, MaxType};  
    }
    
    #endif