Advertisement

class member functions

Started by July 11, 2000 01:24 AM
3 comments, last by Joker2000 24 years, 5 months ago
Keep this code in mind...

class player
{
public:
    player(string, int, int, int, int, int);
    ~player(void);
    void buy_weapon(weapon *, int);
private:
    int weapon_status;
    long money;
};

player::player(params)
{
// stuff
}

player::~player(void)
{ }

void player::buy_weapon(weapon *TheWeapon, int bw)
{
money -= TheWeapon->c_array[bw];
weapon_status = 1;
}

class weapon
{
public:
    weapon(int);
    ~weapon(void);
private:
    int c_array[3];
    int cost;
};

weapon::weapon(int ws)
{
c_array[0] = 0;
c_array[1] = 50;
c_array[2] = 300;

cost = c_array[ws];
}
  
The only reason the above code will not work is because in the function player::buy_weapon(weapon *, int) the class pointer TheWeapon tries to access c_array[bw] . Of course, it can't do this because c_array[] has not been declared yet. So, all I did was move the function player::buy_weapon(weapon *, int) below the declaration of c_array[] and it works no problem. However, I am a neat-freak programmer. I want all the member functions of a class to come right after the class is declared, as shown above. I don't want to have to move them all around the place in weird orders (which is what I'm having to do to fix the c_array[] being declared too late) to get it to compile. Are there any ways to get around this problem without jumbling together all the member functions of multiple classes? At this point, I can't think of what would do the trick, but maybe there's something. Also, I don't think that putting the entire weapon class first would solve the problem either. I would just be getting the same error, only that instead of the weapon members not being yet declared, the player members would be undeclared and the pointer ThePlayer wouldn't be able to access them - identical problem as before. Although this solution may work on the code above, it won't in my real program - trust me. I've got quite a few members (data and functions) that must be declared by class player first before being used by class weapon. --- Joker2000 Stevie Ray Vaughan - The Legend Edited by - Joker2000 on 7/11/00 1:29:25 AM
Well, most people use header files for the class declaration and .cpp files for the class implementation.

You can get around your problem by using forward referencing.

For example, below, you get an error because class B isn't declared.

class A { B m_B; }
class B { A m_A; }

But you can forward reference class B:

class B;
class A { B m_B; }
class B { A m_A; }

I haven't ever tried it, but you might be able to forward reference a class member variables as well. Something like:

int B::m_A; // Forward reference 'int' variable 'm_A' in class B.

Don't hold me to that though.. I would suggest using the standard header/implementation format. Place all your declarations in the header file, and include it at the top of your implementation file.

// CHRIS



Edited by - win32mfc on July 11, 2000 2:39:18 AM
// CHRIS [win32mfc]
Advertisement
Yeah, I might try that. Thanks. BTW, I am going to put all of this "header" stuff in a header file for the final compile. While in design mode, however, I keep EVERYTHING in the .cpp file - it makes it easier to change the "header" components when you don''t have to deal with multiple files (game.cpp, game.h). Then, for the final run, a simple cut-and-paste and voila...there''s your header file!


---
Joker2000
Stevie Ray Vaughan - The Legend
Good luck with that. I''d say it''s easier to develop with multiple files, especially when you get into projects that are 100,000 lines+. But if you''re into the "one-source-file" style of torture, be my guest.

(trust me, you''re in for less pain in the long run if you split up the files now.)
Ok, let''s say I broke my source code apart into a .cpp and a .h (e.g. game.cpp, game.h). Can I break down game.cpp even further to keep related items together, like: game.cpp, weapons.cpp, ai.cpp, sound.cpp, etc.? Of course all of these seperate .cpp files would have their own headers as well. If I am able to break my program into multiple .cpp files, how would I go about compiling them all into ONE file? I''m using Borland C++Builder, if it makes a difference.


---
Joker2000
Stevie Ray Vaughan - The Legend

This topic is closed to new replies.

Advertisement