Hey all,
I'm having some problems trying to access an instance of a class. Rather then try to fill you in, I'm just going to post my code and show you. (Its short and for a console window, since I'm not implementing it into any sort of game anytime soon.... not with these "r337" C++ skills.. lol): Heres the basic run-down. I have a class declared in one file, I make an instance of that class in another file and then I want to access it in another file.
The problem that arises, is this:
c:\programming\c++\console apps\combat\main.cpp(12) : error C2065: 'world' : undeclared identifier
c:\programming\c++\console apps\combat\main.cpp(12) : error C2228: left of '.terrainDisplay' must have class/struct/union type
So here goes..
First, this is the decleration of my Terrain and its functions...
terrain.h
// CTerrain handles all land features, from drawing it to creating it.
class CTerrain
{
public:
CTerrain();
~CTerrain();
void terrainGenerate(void);
void terrainDisplay(void);
int iHeightMap[640];
};
terrain.cpp
#include "terrain.h"
#include "util.h"
#include <iostream.h>
CTerrain :: CTerrain()
{
CTerrain :: terrainGenerate();
}
CTerrain :: ~CTerrain()
{
}
void CTerrain :: terrainGenerate(void)
{
int iHeight = 240; // Middle of the terrain
for(int iLoop = 0; iLoop <= 640; iLoop++)
{
iHeightMap[iLoop] = iLoop; // Do some random stuff here
}
return;
}
void CTerrain :: terrainDisplay(void)
{
for(int iLoop = 0; iLoop <= 640; iLoop++)
{
cout << iHeightMap[iLoop];
}
return;
}
In init.cpp and init.h, I basically make an instance of the class. Right now its bland, but later on it will set up the number of players, etc.
init.h
// Initilization
void InitGame(void);
init.cpp
// Initilization of all important game data
#include "init.h"
#include "terrain.h"
void InitGame(void)
{
CTerrain world;
return;
}
And now in main.cpp, this is where I want to access the world instance.
main.cpp
// Combat War Sim... I guess =P
#include <iostream.h>
#include "terrain.h"
#include "util.h"
#include "init.h"
int main(void)
{
InitGame();
cout << "Blah!\n";
world.terrainDisplay();
return 0;
}
So any ideas why I get the erorr above, and what I can go about to fixing it? And maybe what would be the most memory effective method aswell? (Not as big a deal, though)
Thanks guys.
Edited by - Estauns on February 21, 2002 5:28:07 PM