🔒 Locked
Passing object pointers HELP
Advertisement
Original Post
I'm confused as to why this doesn't seem to work, could someone please help!
Ok, I have 3 files: Main.cpp, Ball.h, Ball.cpp.
Here is my class in Ball.h
class BALL
{
public:
void InitBall(BALL*);
void EraseBall(BALL*);
void MoveBall(BALL*);
void DrawBall(BALL*);
void CollisionDetection(BALL*);
private:
All my member data is here
};
In Ball.cpp are my function implementations
For example here is the one for DrawBall:
//Draws the ball to the screen
void BALL::DrawBall(BALL &Ball,HDC hdc)
{
//Select red colors to draw ball
SelectObject(hdc,Ball.red_pen);
SelectObject(hdc,Ball.red_brush);
//Draw the ball
Ellipse(hdc,Ball.x,Ball.y,Ball.x-30,Ball.y-30);
//Delete colors
DeleteObject(Ball.red_pen);
DeleteObject(Ball.red_brush);
} //End DrawBall
And in the file Main.cpp, in WinMain I have made two BALL objects.
BALL Ball1;
BALL Ball2;
And two pointers to the BALL objects:
BALL *pBall1 = &Ball1
BALL *pBall2 = &Ball2
This is how I am calling my functions:
eg. Ball1.EraseBall(&pBall1);
Ball1.MoveBall(&pBall1);
Ball1.CollisionDetection(&pBall1);
Ball1.DrawBall(&pBall1);
I am fairly new to OOP and using multiple header files so I think my problem lays there, but I could have made a simple mistake.
These are the errors I am getting:
no matching function for call to `BALL::EraseBall (BALL **,
HDC__ *&)'
candidates are: void BALL::EraseBall(BALL *, HDC__ *)
[Linker error] undefined reference to `BALL::EraseBall(BALL *, HDC__ *)'
I am getting those errors for every function!
Thanks for you time
[edited by - julienX on January 27, 2003 9:34:11 AM]
Hi,
I''m not the worlds best C++ / OO programmer by a long shot, but a few things here confuse me
Why are you passing a pointer to an object to itself? When you create an instance of ball that instance has access to its own unique member data and methods. In your methods i.e. draw ball you use a pointer to the instance you are already in. instead of putting Ball.x Ball.y etc just use x and y and don''t bother passing in the reference to itself.
Ellipse(hdc, x , y , x-30, y-30);
Also shouldn''t you have a pointer in your method definition i.e.
BALL::DrawBall(BALL *Ball,HDC hdc)
Instead of:
BALL::DrawBall(BALL &Ball,HDC hdc)
....As you are passing in the address of a ball. Then you would use the -> operator instead of "." as you are using a pointer to a instance.
That should solve the compilation errors, but I still can''t see the need to pass in a pointer to the object you are currently calling the method on. You should only need a pointer to the HDC, all the details of that Ball instance are accessable already.
Hope this helps. It may be a bit unclear, perhaps some one could clarify it a bit better and correct me If I''m wrong
Cheers,
Dave
I''m not the worlds best C++ / OO programmer by a long shot, but a few things here confuse me
Why are you passing a pointer to an object to itself? When you create an instance of ball that instance has access to its own unique member data and methods. In your methods i.e. draw ball you use a pointer to the instance you are already in. instead of putting Ball.x Ball.y etc just use x and y and don''t bother passing in the reference to itself.
Ellipse(hdc, x , y , x-30, y-30);
Also shouldn''t you have a pointer in your method definition i.e.
BALL::DrawBall(BALL *Ball,HDC hdc)
Instead of:
BALL::DrawBall(BALL &Ball,HDC hdc)
....As you are passing in the address of a ball. Then you would use the -> operator instead of "." as you are using a pointer to a instance.
That should solve the compilation errors, but I still can''t see the need to pass in a pointer to the object you are currently calling the method on. You should only need a pointer to the HDC, all the details of that Ball instance are accessable already.
Hope this helps. It may be a bit unclear, perhaps some one could clarify it a bit better and correct me If I''m wrong
Cheers,
Dave
The actual error:
You are taking the address of the pointer. To get the object it is pointing to you must dereference the pointer, like so: Ball1.EraseBall(*pBall1);
Of course, the design is not very good - by passing the objects as parameters you are not taking advantage of the OOP features of C++ at all. Read up on member functions, the this pointer and its implicit uses, et cetera. (In fact, you're passing the objects twice to each function - once implicitly by calling a non-static member function and once explicitly by their pointers.)
[edited by - Miserable on January 27, 2003 10:11:04 AM]
quote:
class BALL
{
public:
void InitBall(BALL*);
void EraseBall(BALL*);
void MoveBall(BALL*);
void DrawBall(BALL*);
void CollisionDetection(BALL*);
private:
All my member data is here
};
BALL Ball1;
BALL Ball2;
BALL *pBall1 = &Ball1
BALL *pBall2 = &Ball2
Ball1.EraseBall(&pBall1);
You are taking the address of the pointer. To get the object it is pointing to you must dereference the pointer, like so: Ball1.EraseBall(*pBall1);
Of course, the design is not very good - by passing the objects as parameters you are not taking advantage of the OOP features of C++ at all. Read up on member functions, the this pointer and its implicit uses, et cetera. (In fact, you're passing the objects twice to each function - once implicitly by calling a non-static member function and once explicitly by their pointers.)
[edited by - Miserable on January 27, 2003 10:11:04 AM]
There''s a thing I don''t understand in your code:
Why do you create a ball object (BALL Ball1) and then
create a pointer to use it ??! (BALL *pBall1 = &Ball1)
You said you''re new to OOP and you have to know that habits are not the same with it.
Here''s what I advice you:
1)
//avoid things like:
void DrawBall(BALL *)
//why using a pointer to an object when this function is a part of //the object ?!
//instead use the simple thing
void DrawBall ()
//with a declaration like:
void BALL::DrawBall(HDC hdc)
{
SelectObject(hdc,red_pen);
SelectObject(hdc,red_brush);
Ellipse(hdc,x,y,x-30,y-30);
DeleteObject(red_pen);
DeleteObject(red_brush);
}
so here is how I would declare the class
class BALL
{
public:
void InitBall();
void EraseBall();
void MoveBall();
void DrawBall();
void CollisionDetection();
private:
//All your member data is here
};
2) Then use constructors and destructors
with these things you can avoid a calling to
InitBall ()
//A constructor is called when the object (class) is created
//Here''s a declaration:
BALL :: BALL () //note you MUSN''T give parameters
{
//add initialisation stuffs here
}
//a destructor is declared like
BALL :: ~BALL ()
{
//...
}
3) here''s how to create a class (dynamicaly):
BALL *Ball1; //declare a pointer
Ball1 = new BALL; //alocate dynamicaly the class
if(!Ball1)
{
//error the ball cannot be created
//Not enough memory !
}
//note that the constructor is called when new is used
//Then use your Ball functions with -> insted of .
//-> is used for pointed class
Ball1->DrawBall();
Ball1->MoveBall();
...
4) an finally to delete your class use
delete Ball1; //simple !
//note that Ball1 must be a valid pointer
//is Ball1 is NULL you''ll get an error
//so better write
if(Ball1)delete Ball1;
Ball1 = NULL; //be sure it points on nothing now
HOPE IT HELPS !
Good luck for OOP
Why do you create a ball object (BALL Ball1) and then
create a pointer to use it ??! (BALL *pBall1 = &Ball1)
You said you''re new to OOP and you have to know that habits are not the same with it.
Here''s what I advice you:
1)
//avoid things like:
void DrawBall(BALL *)
//why using a pointer to an object when this function is a part of //the object ?!
//instead use the simple thing
void DrawBall ()
//with a declaration like:
void BALL::DrawBall(HDC hdc)
{
SelectObject(hdc,red_pen);
SelectObject(hdc,red_brush);
Ellipse(hdc,x,y,x-30,y-30);
DeleteObject(red_pen);
DeleteObject(red_brush);
}
so here is how I would declare the class
class BALL
{
public:
void InitBall();
void EraseBall();
void MoveBall();
void DrawBall();
void CollisionDetection();
private:
//All your member data is here
};
2) Then use constructors and destructors
with these things you can avoid a calling to
InitBall ()
//A constructor is called when the object (class) is created
//Here''s a declaration:
BALL :: BALL () //note you MUSN''T give parameters
{
//add initialisation stuffs here
}
//a destructor is declared like
BALL :: ~BALL ()
{
//...
}
3) here''s how to create a class (dynamicaly):
BALL *Ball1; //declare a pointer
Ball1 = new BALL; //alocate dynamicaly the class
if(!Ball1)
{
//error the ball cannot be created
//Not enough memory !
}
//note that the constructor is called when new is used
//Then use your Ball functions with -> insted of .
//-> is used for pointed class
Ball1->DrawBall();
Ball1->MoveBall();
...
4) an finally to delete your class use
delete Ball1; //simple !
//note that Ball1 must be a valid pointer
//is Ball1 is NULL you''ll get an error
//so better write
if(Ball1)delete Ball1;
Ball1 = NULL; //be sure it points on nothing now
HOPE IT HELPS !
Good luck for OOP
quote:
Original post by Mescalito
//note that Ball1 must be a valid pointer
//is Ball1 is NULL you''ll get an error
//so better write
if(Ball1)delete Ball1;
Ball1 = NULL; //be sure it points on nothing now
I was under the impression that calling delete on a null pointer was guaranteed to do nothing. It is true, however, that the pointer must not be invalid, i.e. pointing at something that is neither ''null'' nor a valid object of a proper type.
quote:
Original post by funkertronic
Also shouldn''t you have a pointer in your method definition i.e.
BALL::DrawBall(BALL *Ball,HDC hdc)
Instead of:
BALL::DrawBall(BALL &Ball,HDC hdc)
....As you are passing in the address of a ball. Then you would use the -> operator instead of "." as you are using a pointer to a instance.
Yup!
My guess is that you read something about this pointers and thought you would have to do it yourself. The way I would do it (limited to DrawBall):
Oh, and even if those two DeleteObject() calls are correct, you still have to restore the previous pen/brush of the HDC (the previous objects are returned by the SelectObject function).
[edited by - BitMaster on January 27, 2003 11:14:23 AM]
class BALL
{
public:
// Blabla
void DrawBall(HDC hdc);
// Blablab
private:
All my member data is here
};
void BALL::DrawBall(HDC hdc)
{
//Select red colors to draw ball
SelectObject(hdc, red_pen);
SelectObject(hdc, red_brush);
//Draw the ball
Ellipse(hdc, x, y, x-30, y-30);
/* Not sure why you do this. You didn't create the objects in this method and like this the
ball would be painted no more than once because its pen and brush would be invalid on the second paint.
*/
//Delete colors
//DeleteObject(red_pen);
//DeleteObject(red_brush);
} //End DrawBall
Oh, and even if those two DeleteObject() calls are correct, you still have to restore the previous pen/brush of the HDC (the previous objects are returned by the SelectObject function).
[edited by - BitMaster on January 27, 2003 11:14:23 AM]
Thanks everyone, but I''m fine now.
How could I have not seen this!
I must have been half-asleep when I wrote this code!
All of you are right, this code sucks!
This probably has to be the worst mistake I''ve ever made! lol.
Sorry for bothering all of you with this ridiculous post
I fixed up my error this morning and it works perfectly, so sorry (and thanks) once again!
How could I have not seen this!
I must have been half-asleep when I wrote this code!
All of you are right, this code sucks!
This probably has to be the worst mistake I''ve ever made! lol.
Sorry for bothering all of you with this ridiculous post
I fixed up my error this morning and it works perfectly, so sorry (and thanks) once again!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement