为什么我收到错误 C2061(语法错误:标识符)?

发布于 2024-10-03 06:35:48 字数 1629 浏览 7 评论 0原文

好吧...我已经和这个错误斗争了至少一个小时了。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-10-10 06:35:48

如果包含 Component.h,那么声明 class Component; 的意义何在?您不能从不完整的类型继承,因此此声明没有意义。

一个疯狂的猜测是循环包含问题,但如果没有更多信息,真的很难判断。我建议阅读这篇关于组织代码的文章如果您对此主题不清楚,请使用 C 和 C++ 编写的文件。

编辑:

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

您无法将类型 StaticSprite 传递给方法。这可能就是错误的根源。向我们展示 addComponent 的声明。第一个参数类型是什么?

编辑:

void Entity::addComponent(TYPE 类型,void* 组件)

好的,那么 TYPE 的定义是什么?我猜这只是 int 的 typedef 或者其他什么?在这种情况下,很明显您不能将 StaticSprite 类型分配给 int

编辑:

enum TYPE{Base = 0, StaticSprite, DynamicSprite, 
     物理、碰撞、输入、粒子、音频、可编写脚本、MaxType};

是的,你就有了。 枚举器 TYPE::StaticSprite类型 StaticSprite 完全无关,尽管它们具有相同的属性姓名。为枚举器指定其他名称,例如 T_StaticSprite,下面的代码应该可以工作:

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

(整个类型枚举是否是一个好主意是另一个问题。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文