Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kyger

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

This topic is 5338 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

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
[ 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
{
// stuff

void cool();
};

Level mylevel;

class Player
{
//stuff

void 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]

Share this post


Link to post
Share on other sites
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);
};

// globals

CEngine game; // player is defined inside CEngine

CLevel level;

// main

int main(int argc, char *argv[]) {

game.init(640, 480, 0);
game.loop();

return 0;
}

// functions and methods

bool 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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!