'Enemy' was not declared in this scope?

Started by
10 comments, last by mrchrismnh 13 years ago
Okay, so thats my error: 'Enemy' was not declared in this scope.The error is in the map.h file, even though map.h includes enemy.h as shown


#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "enemy.h"

#define MAX_TILE_TYPES 20

using namespace std;

class Map{
public:
Map();
void loadFile(string filename);
int** tile;
int** ftile;
bool solid[MAX_TILE_TYPES];
int width;
int height;
int tileSize;

vector<Enemy> enemies;

};

#endif // MAP_H_INCLUDED



And here is enemy.h


#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED

#include "global.h"
#include "map.h"

class Enemy{
public:
Enemy();
Enemy(float nx, float ny, float nstate);
void update(Map lv);
bool rectangleIntersects(float rect1x, float rect1y, float rect1w, float rect1h, float rect2x, float rect2y, float rect2w, float rect2h);
void update();
float x;
float y;
Vector2f velo;
float speed;
float maxFallSpeed;
int state;
int frame;
int width;
int height;

int maxStates;
int *maxFrames;

int frameDelay;

bool facingLeft;
bool onGround;

bool dead;
int drawType;
};

#endif // ENEMY_H_INCLUDED



Anyone know whats going on and how to fix it?
Advertisement
Capital E
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

Does enemy.h need map.h or the other way around? You should make up your mind. Otherwise someone will include "enemy.h", which will include "map.h", which will try to include "enemy.h", but this will not do anything because you are using include guards, then the compiler will get to the point where Enemy is used, but it hasn't been defined yet (since we haven't seen the majority of "enemy.h").

You could make Enemy::update take a constant reference to a map, instead of a map by value, and then instead of including "map.h" you can get away with a "forward declaration" (look it up).

"global.h" also smells funny... although it probably isn't responsible for this particular problem.

Does enemy.h need map.h or the other way around? You should make up your mind. Otherwise someone will include "enemy.h", which will include "map.h", which will try to include "enemy.h", but this will not do anything because you are using include guards, then the compiler will get to the point where Enemy is used, but it hasn't been defined yet (since we haven't seen the majority of "enemy.h").

You could make Enemy::update take a constant reference to a map, instead of a map by value, and then instead of including "map.h" you can get away with a "forward declaration" (look it up).

"global.h" also smells funny... although it probably isn't responsible for this particular problem.



No. Capital E.
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy


Capital E

What do you mean?
[color="#880000"]#include [color="#008800"]"enemy.h"


and then...

vector[color="#666600"]<[color="#660066"]Enemy[color="#666600"]> enemies[color="#666600"];
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy


[color="#880000"]#include [color="#008800"]"enemy.h"


and then...

vector[color="#666600"]<[color="#660066"]Enemy[color="#666600"]> enemies[color="#666600"];



If you don't know what you are talking about, you don't have to say anything.
Mrchrismnh, the name of the header and the name of the class within the header are unrelated...:blink:

The problem is a circular include (map includes enemy and enemy includes map).

If your CPP file includes map first, this happens:#ifndef MAP_H_INCLUDED // test passes, we enter the #if
#define MAP_H_INCLUDED

#include "enemy.h"
//---
#ifndef ENEMY_H_INCLUDED // test passes, we enter the #if
#define ENEMY_H_INCLUDED

#include "map.h"
//---
#ifndef MAP_H_INCLUDED // test fails, we don't enter the #if

#endif
//--

class Enemy{
void update(Map lv);//Error - map hasn't been declared yet

If your CPP file includes enemy first, this happens:#ifndef ENEMY_H_INCLUDED // test passes, we enter the #if
#define ENEMY_H_INCLUDED

#include "map.h"
//---
#ifndef MAP_H_INCLUDED// test passes, we enter the #if
#define MAP_H_INCLUDED

#include "enemy.h"
//---
#ifndef ENEMY_H_INCLUDED // test fails, we don't enter the #if

#endif
//--

class Map{
vector<Enemy> enemies;//Error - enemy hasn't been declared yet

See Alvaro's post and Organising code files in C and C++.
OH fuuuuuuu I got rated down!!
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

Oh shiiiiiii I got rated down even more now I'll never get hired on to one of those MMO teams and those pay big money once the final project is completed and you get a share of the subscriptions!
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

This topic is closed to new replies.

Advertisement