1

So I'm having a problem where I always seem to be getting the location of the last ball drawn when I'm requesting the location. As though the values in the objects are not being assigned properly when I'm declaring the object.

Here is what I have at the moment

ball TestBall[2];

Declaring variables inside a method

void ball::Placeball() {
    //TEMP TEST
    TestBall[1].DrawBall(5,0.15,10,3,10);

    //TEMP TEST
    TestBall[2].DrawBall(5,0.15,10,3,30);
}

My Draw Ball Method and how the variable is being passed

void ball::DrawBall(float radius, float mass, float x, float y, float z) {
    xPos = x;
    yPos = y;
    zPos = z;

    GLUquadricObj *quadratic;
    quadratic = gluNewQuadric();

    glPolygonMode(GL_FRONT,GL_FILL);
    glPolygonMode(GL_BACK,GL_FILL);
    gluQuadricNormals(quadratic,GLU_SMOOTH);

    glTranslatef(x,y,z);
    gluSphere(quadratic,radius,20,20);
    glTranslatef(-x,-y,-z);
}

The passing variable I can't get to work

float ball::zPos

and how it's being retrieved

float ball::GetZ() const {
    return zPos;
}

And then how I'm simply trying to get the value

cout << TestBall[1].GetZ() << endl; //should be 10
cout << TestBall[2].GetZ() << endl; //should be 30

ball.cpp:

float ball::xPos = 0;
float ball::yPos = 0;
float ball::zPos = 0;

ball::ball() { };

void ball::DrawBall(float radius, float mass, float x, float y, float z) {
    xPos = x;
    yPos = y;
    zPos = z;

    GLUquadricObj *quadratic;
    quadratic = gluNewQuadric();

    glPolygonMode(GL_FRONT,GL_FILL);
    glPolygonMode(GL_BACK,GL_FILL);
    gluQuadricNormals(quadratic,GLU_SMOOTH);

    glTranslatef(x,y,z);
    gluSphere(quadratic,radius,20,20);
    glTranslatef(-x,-y,-z);
}

float ball::GetX() const {
    return xPos;
}

float ball::GetY() const {
    return yPos;
}

float ball::GetZ() const {
    return zPos;
}

ball.h:

#pragma once
class ball
{
private:
    static float xPos;
    static float yPos;
    static float zPos;

public:
    ball();
    ball(float radius, float mass, float x, float y, float z){};
    ~ball(){};
    static void DrawBall(float radius, float mass, float x, float y, float z);
    static void UpdateBall(float speedx, float speedz);
    static void Placeball();


    float GetX() const;
    float GetY() const;
    float GetZ() const;
};

Both values read 30, this is the same problem if I increase the size of the array of objects.

Chances are it is something simple I'm missing.

Thanks for your time.

Dori
  • 915
  • 1
  • 12
  • 20
Rodney
  • 127
  • 10

1 Answers1

2

In C++, array indexing is 0-based, so valid indices for ball TestBall[2]; are TestBall[0] and TestBall[1]. Accessing TestBall[2] invokes undefined behavior, and writing to it certainly causes memory corruption.

EDIT: (In response to the question being edited to show ball's definition)

Remove static from xPos, yPos, zPos, DrawBall, UpdateBall, and Placeball and the behavior should be as you expect. You'll need to initialize xPos, yPos, and zPos in ball's constructor instead of at namespace scope.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Have updated my code with this, thanks for spotting - slipped my mind, the problem is still persisting though. Thanks for commenting! – Rodney Apr 22 '11 at 22:26
  • @Rodney : Can you edit your question to show `ball`'s class definition please? – ildjarn Apr 22 '11 at 22:29
  • @ildjarn Done. Included .cpp and .h to clarify anything you may need – Rodney Apr 22 '11 at 22:35
  • @Rodney : Answer edited. Making all your class members `static` is the problem. – ildjarn Apr 22 '11 at 22:39
  • @ildjarn Thanks a lot! and thanks so much for the quick response, I guess it is bad practice on my behalf to practically make everything static - could you iterate to me what the problem was with declaring them static? – Rodney Apr 22 '11 at 22:44
  • @Rodney : Quoting [this FAQ](http://stackoverflow.com/q/408670/636019), "*A static variable is basically a global variable, even if you cannot access it globally. Usually there is an address for it that is in the executable itself. There is only one copy for the entire program. No matter how many times you go into a function call (or class) (and in how many threads!) the variable is referring to the same memory location.*" So, if you want each instance of `ball` to have its own data rather than sharing that global (`static`) data, the data members must not be `static`. – ildjarn Apr 22 '11 at 22:50