Sign in to follow this  
PrestoChung

C++ Scope Probleem?

Recommended Posts

I'm trying to render some variables to the screen. But they are not being updated in the display even though they are working in their appropriate functions. Here is my Player namespace:
namespace Player	{

static float AngleH;
static float AngleV;

static int deltaMove = 0;

static float posX = 0;
static float posY = 2.0;
static float posZ = 0;

static float viewX = 0;
static float viewY = 0;
static float viewZ = 0;

static float speed = 1;

}
If I take away 'static' I get error LNK2005 in the various object files that refer to the Player variables. (It also seems true that I have to #include the Player.h header in any other source files that use namespace Player?) My render function has no problem accessing the variables and printing their initial values with this example code for my horizontal and vertical view angles:
	char a[30];
	sprintf_s(a, "Player::AngleH:%4.3f", Player::AngleH);
	renderOrthoBitmapCharacter(5,43,(void *)GLUT_BITMAP_8_BY_13,a);

	char b[30];
	sprintf_s(b, "Player::AngleV:%4.3f", Player::AngleV);
	renderOrthoBitmapCharacter(5,56,(void *)GLUT_BITMAP_8_BY_13,b);
Now here's the function registered with the mouse to change AngleH and AngleV which is currently working fine:
void mouseMove(int x, int y) 
{
	using namespace Player;
	AngleH += 0.01 * 0.5 * width - 0.01 * x  ;
	AngleV += 0.01 * 0.5 * height - 0.01 * y ;
	orientMe(AngleH, AngleV);
	if (x != 0.5 * width || y != 0.5 * height)
		glutWarpPointer(0.5 * width,0.5 * height);
}
I can look around, but AngleH and AngleV are showing up 0.0 in the display.

Share this post


Link to post
Share on other sites
isn't having a static member inside a namespace redundant? It should have
internal linkage. If you want external linkage, i.e be able to use that variable
in different file, then use the keyword extern like extern float AngleH; But then
you need to define it before you use it like so : float AngleH = 0; Then that
variable has external linkage, I am guessing thats what you needs and thus you
would be able to update it.

Share this post


Link to post
Share on other sites
I got it working according after referring to SiCrane's link (thanks), defined all the variables in the player.cpp file and left just declarations in the namespace.


Quote:
Original post by mmakrzem
Instead of using namespaces, put all your variables into a class called Player. No need to make the variables static either.


Yes I was on my way to making a class but I wasn't quite there yet. I'm not sure how it's supposed to work though, I tried it before. Do I have to call a default constructor and create an instance of the class somewhere in the main body of the code before any functions start changing these variables?

Share this post


Link to post
Share on other sites
Quote:
Original post by PrestoChung
Do I have to call a default constructor and create an instance of the class somewhere ...


Yes. (Well, it doesn't have to be a default constructor; you just need an instance of the class somewhere.)

Quote:
Original post by PrestoChung
... in the main body of the code before any functions start changing these variables?


No. The problem here is that those "any functions" won't be able to see that instance of the class in that body of code.

Like so:

// Sample class:
class Player
{
public:
int mHitPoints;

Player( int hp ) :
mHitPoints( hp )
{
}
};

// Forward declarations:
void FoobarBad();
void FoobarAlternate();
void FoobarGood( Player& );

// Global player for FoobarAlternate demonstration:
Player globalPlayer( 45 );

int main()
{
// Instantiate a player:
Player thePlayer( 45 );

// Attempt to modify "thePlayer"
FoobarBad();
FoobarGood( thePlayer );
FoobarAlternate();
}

void FoobarBad()
{
// Nope.
// ::main::thePlayer.mHitPoints += rand() % 3 + 1;
// thePlayer.mHitPoints += rand() % 3 + 1;
}

void FoobarGood( Player& targetPlayer )
{
// Yup.
targetPlayer.mHitPoints += rand() % 3 + 1;
}

void FoobarAlternate()
{
// Ok. But what if you want to modify a player other than "globalPlayer"?
globalPlayer.mHitPoints += rand() % 3 + 1;
}






Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this