Sign in to follow this  
CyanPrime

'Enemy' was not declared in this scope?

Recommended Posts

CyanPrime    105
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

[code]
#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

[/code]

And here is enemy.h

[code]
#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

[/code]

Anyone know whats going on and how to fix it?

Share this post


Link to post
Share on other sites
alvaro    21266
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
mrchrismnh    82
[quote name='alvaro' timestamp='1302648084' post='4797692']
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.
[/quote]


No. Capital E.

Share this post


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


and then...

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

Share this post


Link to post
Share on other sites
alvaro    21266
[quote name='mrchrismnh' timestamp='1302649813' post='4797702']
[color="#880000"]#include[/color] [color="#008800"]"enemy.h"


and then...

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

[/quote]

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
Hodgman    51339
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:[code]#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[/code]
If your CPP file includes enemy first, this happens:[code]#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[/code]
See Alvaro's post and [url="http://archive.gamedev.net/reference/programming/features/orgfiles/page2.asp"]Organising code files in C and C++[/url].

Share this post


Link to post
Share on other sites
mrchrismnh    82
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
Zahlman    1682
[quote name='mrchrismnh' timestamp='1302713134' post='4798007']
OH fuuuuuuu I got rated down!!
[/quote]

You got voted down because you were [b]factually incorrect[/b]. 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
smasherprog    568
Make this change. Also, you probably want this anyways....

[code]
#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
[/code]

Share this post


Link to post
Share on other sites
mrchrismnh    82
[quote name='Zahlman' timestamp='1303068924' post='4799578']
[quote name='mrchrismnh' timestamp='1302713134' post='4798007']
OH fuuuuuuu I got rated down!!
[/quote]

You got voted down because you were [b]factually incorrect[/b]. 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this