• Advertisement
Sign in to follow this  

'Enemy' was not declared in this scope?

This topic is 2504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
[color="#880000"]#include [color="#008800"]"enemy.h"


and then...

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

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
Hidden
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!

Share this post


Link to post

OH fuuuuuuu I got rated down!!


You got voted down because you were factually incorrect. The system is working as intended.

The rest of your whining (as well as the part where you quoted a post summarizing the correct answer and then didn't actually say anything in response) is unproductive and taking up space. Cut it out. You have been officially warned, and will be punished more harshly the next time you decide to try to create drama. Those posts have been removed.

Share this post


Link to post
Share on other sites
Make this change. Also, you probably want this anyways....


#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED

#include "global.h"
//#include "map.h" <--------------------Commmented this out, it is no longer needed

class Map;//<---------------------Added a forward declaration Now, include map.h in your enemy.cpp file and you are good to go

class Enemy{
public:
Enemy();
Enemy(float nx, float ny, float nstate);
void update(Map* lv); //<---------------CHANGE MADE TO A POINTER. You were creating a copy which was probbly not intended either
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

Share this post


Link to post
Share on other sites

[quote name='mrchrismnh' timestamp='1302713134' post='4798007']
OH fuuuuuuu I got rated down!!


You got voted down because you were factually incorrect. The system is working as intended.

The rest of your whining (as well as the part where you quoted a post summarizing the correct answer and then didn't actually say anything in response) is unproductive and taking up space. Cut it out. You have been officially warned, and will be punished more harshly the next time you decide to try to create drama. Those posts have been removed.
[/quote]

New site looks great by the way!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement