"Base class undefined"

Dec 12, 2014 at 7:05pm
Hi,
I have an error here that's confusing me, it looks like a include-problem.
I have the following classes:
NPC
Animal (is a NPC)

AI
AnimalAI (is a AI)

Environment (has a list of Animal pointer)

NPC has a pointer to AI and AI has one to NPC.
Animal inherits the NPCs AI pointer, but I make it an AnimalAI pointerby overriding the getter, wich should work because AnimalAI is a subclass of AI (co-variant). I do the same with the AI's NPC-getter

There are 26 Errors. Mostly telling that I'm missing ';' before '*'. As this happens in the line where "AnimalAI* getMyAnimal();" is declared, I guess it's just a symptom of this declaration-confusion

In Animal it's "base class is undefined" (so NPC) and an error that occurs beacuse he doesn't accept AnimalAI as an AI, so the co-variant "rule" doesn't work.

Here the code's includes: (Am I missing something, or is the problem bigger?)

Environment:
1
2
3
4
5
6
7
8
9
10
11
12
13

#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include <list>
#include "Animal.h"
#include "Stone.h"
#include <iostream>
using namespace std;
class NPC;

class Environment
{...};
#endif 


NPC:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef NPC_H
#define NPC_H

#include "Object.h"
#include "AI.h"

using namespace std;

class Environment;


class AI;

class NPC{
...
virtual AI* getMyAI();
};

Animal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef ANIMAL_H
#define ANIMAL_H
#include "NPC.h"
#include "AnimalAI.h"

class AI;
class NPC;

class Animal :
	public NPC
{
...
AnimalAI* getMyAI();
};

AI:
1
2
3
4
5
6
7
8
9
10
11
#ifndef AI_H
#define AI_H
#include "Environment.h"
class Environment;
class NPC;

class AI
{
...
virtual NPC* getMyAnimal();
}


AnimalAI:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ANIMALAI_H
#define ANIMALAI_H

#include "AI.h"
#include "Animal.h"
class Environment;

class AnimalAI :
	public AI
{
...
Animal* getMyAnimal();
};


thanks for help,
nonsence90
Dec 12, 2014 at 7:28pm
Headers and Includes: Why and How http://www.cplusplus.com/forum/articles/10627/ (4.- The "right way" to include)
Dec 12, 2014 at 7:45pm
In AnimalAI.h, you included Animal.h which in turn includes AnimalAI.h. You should fix your design on which to include which to avoid this kind of scenario wherein a header includes a particular header file which also includes back the header file that includes it. It's like facing 2 mirrors each other where in you can see unending reflection.
Dec 12, 2014 at 8:23pm
So this error can't be solved directly?
Dec 12, 2014 at 8:48pm
Try to do a workaround:

in AnimalAI.h
- Remove #include "Animal.h"
- Change Animal* getMyAnimal to void* getMyAnimal().
Dec 12, 2014 at 8:54pm
> So this error can't be solved directly?
¿what do you consider a direct solution?
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ANIMALAI_H
#define ANIMALAI_H

#include "AI.h"
// #include "Animal.h" //no need for this
class Animal;
class Environment; //¿what for?

class AnimalAI :
	public AI
{
Animal* getMyAnimal(); //a pointer, a forward declaration will suffice
};
(similar for the others)
Dec 12, 2014 at 9:05pm
@ne555 I ment something like "remove/add line xy". A solution of the problem, not a solution of the error.

@tanezavm For now, I added the AI to the NPC and it works, but it's bothering me how there is no possibility to build something like that?
Dec 12, 2014 at 9:34pm
nonsence90 wrote:
A solution of the problem, not a solution of the error.
They are one and the same.
Dec 12, 2014 at 10:03pm
Maybe I didn't make myself clear. With direct solution I ment the correction of some mistake I made in my code. While a "indirect solution" would be the rebuilding of the whole situation.

So if your answer would have been "you forgot xy on line 5" and I added it it would be a direct solution. The solving of a mistake I made.

But if your answer would have been "it is impossible to do it like that" and I put AI's functionality into the NPC I solved it indirectly. So the mistake was in the design, not in the actual code.

Do you understand what I'm trying to say?
Dec 12, 2014 at 10:20pm
It's generally not a good idea to make up terminology on the fly when you need help ;)
Dec 12, 2014 at 11:59pm
If this thread had a 'like' button i'd be pressing it.
Topic archived. No new replies allowed.