Archived

This topic is now archived and is closed to further replies.

Simple OOP help?

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

Hi, I''m just working on my first OpenGL test program, building on the OOP framework on this site. I''m still very new to classes and objects in general so I''d really appreciate any help. I''ve made a new class for a box object, and it has the x and z coordinates stored as member vars inside it. I''ve made an array of pointers to these objects so I can access these vars as follows: theXCoordinate = boxes[boxIndex]->x; This works fine inside WinMain(), but when I try to access the array of pointers inside the DrawGLScene() func inside the OpenGL class it cannot see them (undeclared identifier). I''ve tried making the array outside of any functions so it is global, but I can''t get the DrawGLScene() function to see them. Any help would be vastly appreciated, considering it''s such a simple problem I''m sure! Thanks in advance, Ben

Share this post


Link to post
Share on other sites
Ok...

// this is my class
class box
{
public:
box();
virtual ~box();
void setPosition(int newX, int newZ) {this->x = newX; this->z = newZ;}
void setRotation(int newRot) {this->rot = newRot;}
int getx() {return this->x;}
int getz() {return this->z;}
int getrot() {return this->rot;}

private:
int x;
int z;
int rot;

};

...

// this is global
box * boxes[numOfBoxes];

...

// this is in WinMain()
for (int boxCount=0; boxCount < numOfBoxes; boxCount++)
{
boxes[boxCount] = new box;
}

...

// This is in OpenGL.DrawGLScene()
glTranslatef(boxes[boxCount]->getx(),0.0f,boxes[boxCount]->getz());

Any ideas?

Ben

Share this post


Link to post
Share on other sites
pass a pointer to the "box" object to the opengl scene like so:

in main.cpp: GLDrawScene( &box)

In the opengl.h change the declaration and implement the function like so:
bool opengl::GLDrawScene( box *myBox ) {

myBox->x etc...

}

If you use VC++ 's "Add member function" Class Wizard will add the line
#include "box.h" to your opengl.h file for you but if not just add it yourself.

if you pass the box instead of a pointer to the box you'll cause an Assert to occur.

Encapsulation is trying to AVOID having your class GLDrawScene depend on anything other than IT's members. That's why your class has no knowledge of global vars outside it. Plus you've declared it in a file the OpenGL.cpp has no knowledge of. (Where does your Opengl.h say to include Winmain.cpp ???? )

hope that helps,
-fs


http://www.CornflakeZone.com
//-- Modelling and animation in every dimension --//


[edited by - fshana on July 24, 2002 10:27:20 AM]

Share this post


Link to post
Share on other sites
Ok I''ll give that a go. It makes sense, although I could not figure out why it wasn''t using the pointer array as it was global. I see it''s because it was in different files now. Thanks a lot for your help,

Ben

Share this post


Link to post
Share on other sites
As I''m passing an entire array as a parameter, not just one pointer, I''ve made my function declarations look like this:

bool DrawGLScene(box *boxes[]);

In main() I''m calling the function with:
Game.DrawGLScene(&boxes[])

Is this correct? I''m getting errors all over the shop to do with my "[]" at the end of the call... Removing the "[]" gives me "error C2664: ''DrawGLScene'' : cannot convert parameter 1 from ''class box *(*)[10]'' to ''class box *[]''"

I''m sorry to bother you with such trivial questions, but I am learning!

Ben

Share this post


Link to post
Share on other sites
This is incorrect.

This declaration
bool DrawGLScene(box *boxes[]);
means a pointer to a pointer it''s the same as writing:
bool DrawGLScene(box **boxes);

you WANT to declare this :
bool DrawGLScene(box *boxes );

quote:

In main() I''m calling the function with:
Game.DrawGLScene(&boxes[])



That''s wrong too. The & gives a reference to the memory address the pointers value is at.

e.g. int myint = 9;
myint''s value is 9, &myint is a reference to the address in which the value 9 is stored. This means *(&myint) is 9;

hope that explains why you need to call your function with this:

Game.DrawGLScene(boxes)

since boxes is declared as an array (which is a pointer) you just pass boxes, not &boxes as I''d previously advised.

Sorry for the wordy response but I''m trying to show you how to fish instead of giving you a fish.

-fs

Share this post


Link to post
Share on other sites
Thanks again for your help,

I understand what you''ve said, in that I pass a pointer to my array so I can in effect access the array from the DrawGLScene() function.

However, I''m not entirely crystal on why I just pass boxes instead of &boxes. If I the function is expecting a pointer to an array of pointers then shouldn''t I pass the address of the array so the pointer knows where to look?

Also, after doing as you said I cannot use my array of pointers. It''s as if I''ve only passed one subscript of the array, but I need to pass the whole array as it pointers to several box objects.

In other words, I can do box->getx(); with no problems, but I cannot do box(2)->getx(); which is what I really need.

Thanks again,

Ben

Share this post


Link to post
Share on other sites
a view of pointers (really confusing to begin with, but very important)


ok:


in this code, you have 1 object.


CMyObject object;



in the following code you have 2 objects:

CMyObject objects[2];


But. This isn''t actullay what it appears to be. objects is NOT an array, it is a pointer.

The best way to discribe a pointer is this analogy.

imagine you have a book full of statistical records (or whatnot).
Each page can have 1 or more records. A pointer is not the actual record, it is the page number that it is located on. You can say ''record on page 43'' or ''3rd record on page 56'', etc.. But you can''t say that the page it''self is a record.

so...

to convert this into code,
a pointer is the ''page'' in memory where the data is located.


This is where arrays get confusing, as the ''object'' is not really an array, but a pointer.

CMyObject objects[];
is the same as
CMyObject * objects;

it''s just when you use:
CMyObject objects[43];
the compiler will create room for 43 objects, fill the memory, and set ''objects'' to the position in memory that it just filled for you.

So when you use objects[0], it goes directly to that position in memory. Using objects[1] goes to the next object in the block of memory created.
This is why you cannot do:

int number=getNumberINeed();

CMyObject objects[number];


because this is a dynamic memory block (it can be different sizes). To do that, you need to allocate it yourself,

CMyObject * objects = new CMyObject[number];

AND you must delete it when your done with it...

delete [] objects;

otherwise you get memory leaks. (not good)


To access a pointer to an object is very easy. but also dangerous...


CMyObject object;

CMyObject * pointer = &object;

object.doStuff();
pointer->doStuff();


the last 2 lines are very important. That is the reason for having -> and . operators. both those lines of code would do exactly the same thing.

to access the actual object stored where a pointer is:

CMyObject object;
CMyObject object2;

CMyObject * pointer = &object;

object2= (*pointer);


this last line makes the MEMORY where object2 is exactly the same as the memory where object is. Ie, it made object2 a copy of object. (but it did it via a pointer)
it''s the same as doing:

object2=object;


Now,

the problems you were having was that you had a double pointer...

first, you had an array of objects,
eg,

CMyObject objects[25];

but, when you did:

box *boxes[]

effecitivly you were saying:

box ** boxes

that means you were passing in a pointer to another pointer.
Since there is only one ''boxes'' pointer, doing:

boxes[x]->doStuff();

will cause huge problems because you are telling the compiler that ''boxes'' is an array (When it is really only 1 pointer... to the array you intended to use)

the correct thing to do would have been:

(box * boxes, int numBoxes)

since you don''t know how long the array is simply from the pointer to the first object in the array, you need to know how many there are.

I hope that clears a lot of things up.




Share this post


Link to post
Share on other sites