为什么我收到错误 C2061(语法错误:标识符)?
好吧...我已经和这个错误斗争了至少一个小时了。我的 StaticSprite 类继承自 Component 类。但是,当我尝试实例化 StaticSprite 实例时,出现错误 c2016。
这是我的相关代码,如果需要,我会发布更多内容。 提前致谢。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果包含
Component.h
,那么声明class Component;
的意义何在?您不能从不完整的类型继承,因此此声明没有意义。一个疯狂的猜测是循环包含问题,但如果没有更多信息,真的很难判断。我建议阅读这篇关于组织代码的文章如果您对此主题不清楚,请使用 C 和 C++ 编写的文件。
编辑:
您无法将类型
StaticSprite
传递给方法。这可能就是错误的根源。向我们展示addComponent
的声明。第一个参数类型是什么?编辑:
好的,那么
TYPE
的定义是什么?我猜这只是int
的 typedef 或者其他什么?在这种情况下,很明显您不能将StaticSprite
类型分配给int
。编辑:
是的,你就有了。 枚举器
TYPE::StaticSprite
与类型StaticSprite
完全无关,尽管它们具有相同的属性姓名。为枚举器指定其他名称,例如T_StaticSprite
,下面的代码应该可以工作:(整个类型枚举是否是一个好主意是另一个问题。)
What is the point of the declaration
class Component;
if you includeComponent.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:
You cannot pass the type
StaticSprite
to a method. That's probably the source of the error. Show us the declaration ofaddComponent
. What is the first parameter type?Edit:
Okay, so what is the definition of
TYPE
? I guess it's just a typedef forint
or something? In that case it should be clear that you cannot assign the typeStaticSprite
to anint
.Edit:
Yep, there you have it. The enumerator
TYPE::StaticSprite
has absolutely nothing to do with the typeStaticSprite
, even though they have the same name. Give the enumerator some other name, for exampleT_StaticSprite
, and the following code should work:(Whether this whole type enumeration thing is a good idea is a different question.)