C++代理:游戏中基类未定义错误
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来有些东西在定义之前就引用了类
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 inaStar.h
.EDIT: Search for
#define AGENT_H
everywhere in your source. If you find it anywhere outsideAgent.h
, that means theAgent
class might never be defined, if only becauseAgent.h
can be#included
withAGENT_H
already#defined
.不要在
Sinbad.h
中重新声明类Agent
。您已经包含了Agent.h
。Agent.h
中的Grid
和Astar
似乎也是这种情况。Do not redeclare class
Agent
inSinbad.h
. You already includedAgent.h
. Seems also to be the case inAgent.h
withGrid
andAstar
.一些评论表明您不确定前向声明是如何工作的。
当您需要告诉编译器该类型存在但不需要其任何定义时,前向声明该类型。通常,当类的成员或方法仅具有对该类型的指针或引用时,这是在标头中完成的。为了执行涉及取消引用指针或使用引用的任何操作,您需要包含定义类型的标头,通常在定义类方法的源代码中。
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.