C++代理:游戏中基类未定义错误

发布于 2024-10-01 20:27:37 字数 797 浏览 8 评论 0原文

我有 6 个 C++ 头文件。包含的内容很多,所以我尽量少用。但我从一开始就不断收到错误,说类“Agent”未定义。我定义了它并包含它,但找不到问题,这是导致问题的 2 个头文件:

Sinbad.h:

#ifndef SINBAD_H
#define SINBAD_H

#pragma once
#include "Agent.h"

#define NUM_ANIMS 13  // number of animations the character has. Should be made character specific

class Agent;

class Sinbad : public Agent {

Agent.h:

#ifndef AGENT_H
#define AGENT_H

#include <deque>
#include "aStar.h"

extern Ogre::SceneManager* sceneMgr; // Defined in main.cpp

class GridNode; // forward declarations
class Grid;
class Astar;

class Agent {

这是我收到的错误:

1>c\gameengine_solution\sinbad.h(12) : error C2504: 'Agent' : base class undefined

I have 6 C++ header files. There are a lot of includes so I tried to make it so that I use as little as I can. But I keep getting an error from the very beginning saying that a class "Agent" is undefined. I defined it and included it and can't find the problem here are the 2 header files that are causing the problem:

Sinbad.h:

#ifndef SINBAD_H
#define SINBAD_H

#pragma once
#include "Agent.h"

#define NUM_ANIMS 13  // number of animations the character has. Should be made character specific

class Agent;

class Sinbad : public Agent {

Agent.h:

#ifndef AGENT_H
#define AGENT_H

#include <deque>
#include "aStar.h"

extern Ogre::SceneManager* sceneMgr; // Defined in main.cpp

class GridNode; // forward declarations
class Grid;
class Astar;

class Agent {

Here's the error I am getting:

1>c\gameengine_solution\sinbad.h(12) : error C2504: 'Agent' : base class undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

酒绊 2024-10-08 20:27:37

看起来有些东西在定义之前就引用了类 Agent,即可能在 aStar.h 中。

编辑:在源中的任何位置搜索#define AGENT_H。如果您在 Agent.h 之外的任何地方找到它,则意味着 Agent 类可能永远不会被定义,因为 Agent.h 可以是<代码>#included 与AGENT_H#define

It looks like something is referring to class Agent before it is defined, i.e. probably in aStar.h.

EDIT: Search for #define AGENT_H everywhere in your source. If you find it anywhere outside Agent.h, that means the Agent class might never be defined, if only because Agent.h can be #included with AGENT_H already #defined.

戏舞 2024-10-08 20:27:37

不要在 Sinbad.h 中重新声明类 Agent。您已经包含了 Agent.hAgent.h 中的 GridAstar 似乎也是这种情况。

Do not redeclare class Agent in Sinbad.h. You already included Agent.h. Seems also to be the case in Agent.h with Grid and Astar.

婴鹅 2024-10-08 20:27:37

一些评论表明您不确定前向声明是如何工作的。

当您需要告诉编译器该类型存在但不需要其任何定义时,前向声明该类型。通常,当类的成员或方法仅具有对该类型的指针或引用时,这是在标头中完成的。为了执行涉及取消引用指针或使用引用的任何操作,您需要包含定义类型的标头,通常在定义类方法的源代码中。

Some of the comments suggest you're unsure how forward declaring works.

Forward declare a type when you need to tell the compiler that the type exists, but do not need any of its definition. Generally, this is done in a header when a class's members or methods only have pointers or references to the type. In order to do anything that involves dereferencing the pointer or using the reference, you will need to include the header that defines the type, usually in the source that defines the methods of the class.

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