Jump to content
  • Advertisement
Sign in to follow this  
nibbuler

[C++] Cross-Class Variables?

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

I'm trying to place a string in SDL below the player's sprite... That will move with the player. I'm getting this error: LNK2001: unresolved external symbol "public: static int Player::y" for both x and y.
//Edited out the unrelated code
class Player
{
    public:
    static int x, y;
};

Player::Player()
{
    //Initialize the offsets
    x = 0;
    y = 0;
}

void StringInput::show()  
{
    //If the surface isn't blank
    if( text != NULL )
    {
        //Show the name
		apply_surface( Player::x, Player::y + 20, text, screen ); //How  can I call a variable from another class?
    }
}

I can't figure out how to use the x and y variables from Player in StringInput::show(). Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

void StringInput::show( Player *pPlayer )
{
//If the surface isn't blank
if( text != NULL )
{
//Show the name
apply_surface( pPlayer->x, pPlayer->y + 20, text, screen );
}
}



Somewhat like that.

Share this post


Link to post
Share on other sites
Static member variables have to be defined globally outside the class definition (in addition to the declaration within the class).

int Player::x = 0;
int Player::y = 0;

Share this post


Link to post
Share on other sites
Since you have declared the two members to be static, you have to define them somewhere outside the class definition. Like

int Player::x = 0;
int Player::y = 0;


Otherwise, the linker fails to find their symbols and produces your error.

Also, for static members it is dangerous to define a value inside the constructor of their class, because all your Player classes will use the same and every instance of that class would override the actual values. If you have just one instance of your class, this won't be a problem but it is bad style anyway.

As a suggestion for your case: you don't have to declare the variables as static, since they are public (everyone has access). When you do that, you can have more than one Player class, with players who have different positions.

Share this post


Link to post
Share on other sites
The above will work, but it doesn't look like what you want. It seems you're using Player as an instance of a class (ie, everything is static). This is not the done thing. Instead, make an instance of the Player class, and use that:

class Player
{
public:
int x, y;
};

// this pointer should be a boost::shared_ptr
void StringInput::show(Player* player)
{
apply_surface(player->x, player->y + 20, text, screen);
}


What you have now reeks of bad design. I can understand the sentiment (you will only have one player), but this is a case where "just making one" is fine. Hell, if you ever (in the future) implement multiplayer, you may find the way you've written your code will limit you.

Share this post


Link to post
Share on other sites
Alright I hit one other snag. I got rid of the static variables and tried to use the pointer in show(Player* player), but I get error C2511 'void StringInput::show(Player *)' : overloaded member function not found in 'StringInput'.

I figured that I should just define show() in the class as:

class StringInput
{
public:
void show( Player* ); //Tried with and without the * and the second player
};



but then I get a host of errors in my timer class to limit the fps.

Share this post


Link to post
Share on other sites
Post the full class. Are you including player.h, or forward declaring Player? Also consider using a (const) reference instead of a pointer:

class Player;

class StringInput {
public:
void show(const Player &);
};

Share this post


Link to post
Share on other sites
You'll need to show more code for that, the error is probably caused somewhere else. Also, it's helpful to post errors you get, maybe not all of them if its a lot, but at least the first few.

Share this post


Link to post
Share on other sites
Lifted most of my code directly from LazyFoo.

All of this code is in player.h, but called in main.cpp.

class Player
{
private:
//Skills
bool vanish;
int ability [3] [6];

public:
int x, y;

//The velocity of the dot
int xVel, yVel;

//Initializes the variables
Player();

//Takes key presses and adjusts the dot's velocity
void handle_input();

//Moves the dot
void move();

//Shows the dot on the screen
void show();
};

class StringInput
{
private:
//The storage string
std::string str;

//The text surface
SDL_Surface *text;

public:
//Initializes variables
StringInput();

//Does clean up
~StringInput();

//Handles input
void handle_input();

//Shows the message on screen
void show( Player* );
};

void StringInput::show( Player* player )
{
//If the surface isn't blank
if( text != NULL )
{
//Show the name
apply_surface( player->x, player->y + 20, text, screen ); //How to call a variable from another class? }
}




Some of the errors I get when I change it to
void show( Player* player );


, which were working fine before, are in timer.h:


Timer::Timer()
{
startTicks = 0; //error C2065: 'startTicks' : undeclared identifier
pausedTicks = 0; //same with these three
paused = false;
started = false;
}

void Timer::start()
{ //error C2601: 'StringInput::show::Timer::start' : local function definitions are illegal. this line contains a '{' which has not yet been matched
//Don't exactly know how StringInput and Timer classes have connected themselves...
started = true;

paused = false;

startTicks = SDL_GetTicks();
}




Hopefully that should fill in the gaps. :)

Share this post


Link to post
Share on other sites
Quote:

void StringInput::show( Player* player )
{
//If the surface isn't blank
if( text != NULL )
{
//Show the name
apply_surface( player->x, player->y + 20, text, screen ); //How to call a variable from another class? }
}


Your closing brace for the if statement is in a comment, so the compiler continues including files, it thinks they are all inside show(). This confuses it - a lot.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!