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

Started by
6 comments, last by andfol 12 years, 1 month ago
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.
Advertisement
Post some code so we can see what you're doing.
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.
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.
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.
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.
[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]

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.

This topic is closed to new replies.

Advertisement