Jump to content
  • Advertisement
Sign in to follow this  
andfol

Problem with passing values into function with an object intialised in constructor

This topic is 2475 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 have a draw player class which draws a player in OpenGL. Within this class I have a drawPlayer function for the parameters of this function I pass some member variables for position which have been initilised in a constructor. In my main window I create an object and give it values for the positon parameters to call the drawPlayer function. My problem is when I call the drawPlayer function with the object I get an error because it asks for the arguments and does not use the arguments passed in from the object.

Share this post


Link to post
Share on other sites
Advertisement
Okay will try to explain the above with code

Here is my constructor:

CPlayer::CPlayer(float xVal, float yVal, float zVal)
: m_xPos(xVal),m_yPos(yVal),m_zPos(zVal),m_xVel(0.0),m_yVel(0.0),m_zVel(0.0)
{
float armAngle[2] = {0.0f, 0.0f};
float legAngle[2] = {0.0f, 0.0f};// each leg's current angle

}

as you can see I am intialising the position and velocity the arrays are to animate the arms and legs of the player.

This is the signiture of the function that draws the player.

void CPlayer::drawPlayer(float m_xPos, float m_yPos, float m_zPos)

This translation also gets passed the position variables from the parameters within the drawPlayer function.

glTranslatef(m_xPos, m_yPos, m_zPos);

Finally I am calling drawPlayer in main with an object with the values for the parameters.

CPlayer g_Player1 (100.0,100.0, 100.0);

g_Player1.drawPlayer();

The problem is the call to the drawPlayer function asks for the arguments to be passed in explicitly and doesn't take the arguments from the object. This code is to draw a player to the screen.

Share this post


Link to post
Share on other sites
Okay will try to explain the above with code

Here is my constructor:

CPlayer::CPlayer(float xVal, float yVal, float zVal)
: m_xPos(xVal),m_yPos(yVal),m_zPos(zVal),m_xVel(0.0),m_yVel(0.0),m_zVel(0.0)
{
float armAngle[2] = {0.0f, 0.0f};
float legAngle[2] = {0.0f, 0.0f};// each leg's current angle

}

as you can see I am intialising the position and velocity the arrays are to animate the arms and legs of the player.

This is the signiture of the function that draws the player.

void CPlayer::drawPlayer(float m_xPos, float m_yPos, float m_zPos)

This translation also gets passed the position variables from the parameters within the drawPlayer function.

glTranslatef(m_xPos, m_yPos, m_zPos);

Finally I am calling drawPlayer in main with an object with the values for the parameters.

CPlayer g_Player1 (100.0,100.0, 100.0);

g_Player1.drawPlayer();

The problem is the call to the drawPlayer function asks for the arguments to be passed in explicitly and doesn't take the arguments from the object. This code is to draw a player to the screen.

Share this post


Link to post
Share on other sites
If the function signature has parameters then you need to call the function with those parameters. If you want to call it as you are, you'll need to change the function's signature to reflect that. You don't need to specify member variables in the parameter list—the method can already see them.

Share this post


Link to post
Share on other sites
Okay yckx,

What do I need change in the function signiture using my method, I am unsure I would like to call the function with the values from the object.

Thanks.

Share this post


Link to post
Share on other sites
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

void CPlayer::drawPlayer(float m_xPos, float m_yPos, float m_zPos) should be

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

void CPlayer::drawPlayer(). You'll need to change the declaration in the class too.

[/font]

Share this post


Link to post
Share on other sites
Thank You rip-off,

It worked though I should of thought of it myself, next will be movement any ideas I would be grateful for some feedback.

Thanks.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!