Public Group

# Cannot find classes / source files

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

## Recommended Posts

I imagine this problem has something to do with my lack of understanding of using source files. I'm using Allegro and Dev-C++. When I compile the program, player.cpp cannot find class CTileMap's TileMap. I've tried declaring CObject as a friend of CTileMap, moving code around, google and forums for a week and still nothing. Ive stripped it down to what you should need to help. Thanks! main.cpp
#include <allegro.h>
#include "tilemap.h"
#include "player.h"

CTileMap TileMap;
CObject player, enemy[2];

int main(){
}
END_OF_MAIN();


Player.cpp with errors commented

#include "player.h"

void CObject::Collision() {

if (TileMap.begin[2]+10 != 30)     // TileMap' undeclared (first use this function)
{
//do stuff

}
}


player.h
#ifndef __PLAYER_H__
#define __PLAYER_H__

#include <allegro.h>
#include <string>
using namespace std;
#include "tilemap.h"

class CObject {
public:
void Collision();

};

#endif


tilemap.h
#ifndef TILEMAP_H_
#define TILEMAP_H_

#include <allegro.h>
#include <fstream>
using namespace std;
#include "player.h"

extern int width;
extern int height;

class CTileMap {

public:
int begin[3];

};

#endif


[Edited by - Amatamazi on July 27, 2008 5:34:25 AM]

##### Share on other sites
Theres a circular dependency in there (tilemap.h includes player.h which includes tilemap.h).

The easiest way around this would be to use a "Master" header file. The default in VC++ is usually the precompiled header, stdafx.h.

Instead of including allegro's header in every file, just include it in the top file and include that in every file. Perhaps this isn't clear enough, I'll try to code it in:

#ifndef _MASTER_H_INCLUDED_#define _MASTER_H_INCLUDED_#include <allegro.h>using namespace std;#include "tilemap.h"#include "player.h"//etc

and include this in all of your *.cpp files. Hope that makes sense.

##### Share on other sites
I did that but it still gives me the same error. I thought it had something to do with declaring CTileMap TileMap in main.cpp, and player.cpp not seeing it because it was declared after all the source files were included.

##### Share on other sites
Sorry, I was just talking about generally setting out the project.

You could set TileMap as either a global variable in your master header, or as a member of the class you are using it within, depending on how you want to use it. I'm sure there will be other ways of doing it (extern?), perhaps somebody a bit more up on C++ could suggest others?

##### Share on other sites
May-be it cannot be seen because you have stripped much contents out of your headers, but if a header doesn't need to know about another class, it shouldn't include the respective header.

Otherwise you might learn about using forward declarations to break cyclic dependencies.

##### Share on other sites
Maybe my answer is in that link you gave me, I've read that before, but I don't think that's what I'm looking for. I'm not sure if I clearly stated my question.

Player.cpp can't find TileMap (an object of class CTileMap), which is declared in main.cpp. Where do I declare TileMap so that it can be seen by tilemap.cpp, player.cpp, and main.cpp?

Thanks again.

##### Share on other sites

An alternative might be to pass the file map by const reference to the Collision method, or pass the Object instance to a TileMap member function that will report any collisions. This way you can avoid global variables, which is usually a very good idea.

##### Share on other sites
Thank you! I needed to pass CTileMap TileMap to Collision(). Thanks, rip-off, for ending hours of headache.

##### Share on other sites

It's worth knowing that you can get globals from other files using the extern keyword.

Declare a global normally in one file, for example, int numFoo = 0; and then in any other file that needs it, do this around the top:
extern int numFoo;`

Notice I did not repeat the initial value. For global objects with constructors, only write the construction arguments with the 'normal' declaration, not in any of the extern declarations.

All warnings about globals apply, though. I'm only using this for my log.

##### Share on other sites
Interesting article.

I recognise the increase in build times and the rejigging of #include directives, but I've always used a master header as a nice guide to the code. Perhaps it's just the way my brain organises it, but the master header provides a quick and easy guide to the path through the project's code.

Like most programming practices I've come across, it seems like a trade off. Master headers increase build time, make you focus on the compiler's operation more (is this a bad thing though for beginners?) but decrease complexity in the code. Or have I missed the point completely? [grin]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634048
• Total Posts
3015237
×