C++ Agent : Base class undefined error in Game
I have 6 C++ header files. There are a lot of includes so I tried to make it so that I use as lit开发者_高级运维tle 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
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
.
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
.
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.
精彩评论