[C++] Accessing global class from inside a class?

Started by
6 comments, last by Kyger 20 years ago
Lets say I have the following:

struct CTile {
     bool solid;
     SDL_Surface *img;
}

class CLevel {
     public:
          bool collision(int x, int y) {
               return tile[x][y].solid;
          }
     private:
          CTile tile[100][100];
}

class CPlayer {
     public:
          bool colx(int x, int y);
     private:
          // misc stuff down here

}
I declared CLevel level as a global class, and inside bool CPlayer::colx() I need to access the bool collision() function inside of CLevel. I''m doing if(level.collision(50, 50)) and it''s saying that I need to have something to the left of .collision. The exact error is: C:\seishou\core.cpp(522) : error C2228: left of ''.collision'' must have class/struct/union type What am I doing wrong? -------------------------------- The Seishou Project - 1% complete Come support us! It gives us motivation
Advertisement
Is the declaration of CLevel level; before the point where you use it?
You say you made CLevel level global. But, how? Is it in a cpp file or a header? Are you certain the code trying to use it actually has it''s definition?
I made a new project just to test out my collision detection/response, so everything is in 1 file (core.cpp). First I have all the classes defined, then function prototypes, then globals, main, then all the functions/methods under main.
[ let's keep the personal insults out of here, ok, ncsu? -Sneftel ]

you must declare the global variable before you use it

example

class Level{// stuffvoid cool();};Level mylevel;class Player{//stuffvoid do_stuff(){   mylevel.cool();}};



"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

[edited by - sneftel on April 10, 2004 6:02:24 PM]
I know that. The class is declared before I use it. Here''s the exact order in which these things appear in my source file.

struct CTile {	bool solid;	SDL_Surface *img;};class CLevel {	public:		bool collision(int x, int y) {			return tile[x + ((y-1) * mWidth)].solid;		}		void loadmap(char *file);	private:		CTile *tile;};class CPlayer {	public:		bool colx(int x, int y, int &tilecoordy);		bool coly(int x, int y, int &tilecoordx);};// globalsCEngine game;     // player is defined inside CEngineCLevel level;// mainint main(int argc, char *argv[]) {	game.init(640, 480, 0);		game.loop();	return 0;}// functions and methodsbool CPlayer::colx(int x, int y, int &tilecoordy) {	// blah blah	while(xpixels <= testend) {		if(level.collision(tilecoordx, tilecoordy))			return true;                // more stuff	}	return false;}


--------------------------------
The Seishou Project - 1% complete

Come support us! It gives us motivation
Not enough of that code snippet compiles on its own to make any helpful suggestions. Either you can try posting the complete example (since it''s only one file, other people could copy/paste it easily to decipher what''s going on) or you can try making a copy of your project and deleting parts that you know work properly until you get a minimal code snippet that exhibits the problem.
Wow. I can''t believe I didn''t see this earlier. I had an integer in the player class called level so that was causing the problem.

Thanks for the attempted help, however.

--------------------------------
The Seishou Project - 1% complete

Come support us! It gives us motivation

This topic is closed to new replies.

Advertisement