need to pass a C++ object to another class
Hi I have the following:
trajectory class inherits shape{
void center_cpu_paddle_colbox(PaddleC *);
};
void Trajectory::center_cpu_paddle_colbox(PaddleC *){
}
main{
theTraj.center_cpu_paddle_colbox(CpuPaddle);
}
CpuPaddle is a PaddleC object created like so:
PaddleC CpuPaddle;
Now the above code I get a syntax error paddleC ......the thing is I need to be able to pass the PaddleC object as PaddleC (not shape). the reason for this is to do with collision detection and to save me have multile methods checking what kind of shape is being passed e.g ball,paddlep or paddlec.....After all the initialising is done I want to pass say a ball object to trajectory to check it for collision, actually its a ball and a paddle I want to pass but I dont want to get into the details of that .......I just realy need to be able to pass specific objects . I did it by passing shape and it took me like 1 week to figure out why the vector wasnt initialised in the case when I checked if a ball collided with the player paddle and it was to do with passing shape and having multiple ifs and the ball object only knowing about the ball vector and the paddle object knowing only about the paddle vector.
Sorry for the bad explination ^^ its just I really need to know how to pass onjects this way ^ .......I used the same format for above for when I passed this to another class but it isnt working whilst passing the CpuPaddle the above way. I plan to pass all 3 objects to the trajectory class but if someone could show me how to do the above that would get me out a bad situation here :-)
Thanks
David
I have tested my class works.... no that is no where near the full code .......I was getting no errors until I tried this so I no it is my syntax forr passing a PaddleC object into the Trajectory Class also I dont use the word inherits I use :: .......I just want to know how to pass a PaddleC object from main to the Trajectory Class.......I was able to pass a Shape object ok, the only difference being is that Trajectory inherits from shape
//my codes just been disected but I will submit some of main
but the issue is 100% the code I submitted - excluding the inherits word
(there maybe some issues with my code now since Ive altered it since the post but my question still remains the same
Thanks for the reply :-) I can already pass a shape object and I know that would be the correct way but I want to be able to pass any object to any class I desire ....I know this is not ideal but I need to at least learn to do it all the same and in this case it will make things easier and I can get my 1st project finished
//my codes just been disected but I will submit some of main
but the issue is 100% the code I submitted - excluding the inherits word
(there maybe some issues with my code now since Ive altered it since the post but my question still remains the same
Trajectory theTraj;PaddleP PlayPaddle;PaddleC CpuPaddle;Ball GameBall;fps.start();float delta = 0;unsigned long currentT = 0, prevticks = SDL_GetTicks(); //game loop while(quit == false) { //do condiotion via event poller to handle user events //while there events to handle //there will be no events first time round so this while will be skipped initially currentT = SDL_GetTicks(); //printf("prevticksis is : %f\n",prevticks); delta = (currentT - prevticks) / 1000.0f; prevticks = currentT; while(SDL_PollEvent(& event) ) { //handle events for the user movable dot PlayPaddle.handle_events(); // CpuPaddle.handle_events(); //GameBall.handle_events(); //PlayPaddle.setSpeed(pausetick_start); //if the user has Xed out the window if(event.type == SDL_QUIT) { //set quit flag to true quit = true; } } //CpuPaddle.applyVelocity(); CpuPaddle.applyVelocity(); theTraj.center_cpu_paddle_colbox(CpuPaddle);//<<<<main closes more code etcclass Shape {public:#define TYPE_PADDLEP 1#define TYPE_PADDLEC 2#define TYPE_BALL 3Shape();std::vector<SDL_Rect>colBox_ball;std::vector<SDL_Rect>colBox_ppaddle;std::vector<SDL_Rect>colBox_cpaddle;int type;float width;float height;bool isStartTraj;float speed;float cx;float cy;float x;float y;float xvel;float yvel;float START_YP;float START_XP;float START_YC;float START_XC;float START_XBALL;float START_YBALL;float getSPositionCPaddleX();float getSPositionCPaddleY();void setWidth(float);void setXvel(float);void setYvel(float);void setX(float);void setCurrentX(float);void setCurrentY(float);float getX();float getY();float getWidth();float getHeight();int getClassT();void setSpeed(float);void setClassT(int);};class Trajectory: Shape{public: float speed;Trajectory(Shape *to);Trajectory();void center_cpu_paddle_colbox(PaddleC *CpuPaddle);};void Trajectory::center_cpu_paddle_colbox(PaddleC *CpuPaddle){}
Thanks for the reply :-) I can already pass a shape object and I know that would be the correct way but I want to be able to pass any object to any class I desire ....I know this is not ideal but I need to at least learn to do it all the same and in this case it will make things easier and I can get my 1st project finished
when you pass the paddle class you need to use the address of operand '&' such as:
Function(&CPaddle);
so change
theTraj.center_cpu_paddle_colbox(CpuPaddle);
to
theTraj.center_cpu_paddle_colbox(&CpuPaddle);
Function(&CPaddle);
so change
theTraj.center_cpu_paddle_colbox(CpuPaddle);
to
theTraj.center_cpu_paddle_colbox(&CpuPaddle);
Instead of pointers your could also use a reference, which in this case would be better. When you are using a pointer, you actually should check if the pointer is valid (not NULL). In this case you expect a paddle, and it always must have one. In that case you better use a reference, which forses you to give it the object otherwise the compiler will complain. Advantage also is that you dont have to worry about NULL pointers and & operators etc.
PaddleC is a class derived from shape EkEk.
Thanks everyone I will try those suggestions jeroenb and speedie, this is exactly the kind of replies I was hoping for thanks very much!
edit-----
Im actually still getting the same error
heres the shape parent class to trajectory,ball,paddlep and paddlec:
heres the trajectory class:
heres the PaddleC class:
heres main:
thanks
David
Thanks everyone I will try those suggestions jeroenb and speedie, this is exactly the kind of replies I was hoping for thanks very much!
edit-----
Im actually still getting the same error
heres the shape parent class to trajectory,ball,paddlep and paddlec:
class Shape {public:#define TYPE_PADDLEP 1#define TYPE_PADDLEC 2#define TYPE_BALL 3Shape();std::vector<SDL_Rect>colBox_ball;std::vector<SDL_Rect>colBox_ppaddle;std::vector<SDL_Rect>colBox_cpaddle;int type;float width;float height;bool isStartTraj;float speed;float cx;float cy;float x;float y;float xvel;float yvel;float START_YP;float START_XP;float START_YC;float START_XC;float START_XBALL;float START_YBALL;float getSPositionCPaddleX();float getSPositionCPaddleY();void setWidth(float);void setXvel(float);void setYvel(float);void setX(float);void setCurrentX(float);void setCurrentY(float);float getX();float getY();float getWidth();float getHeight();int getClassT();void setClassT(int);};
heres the trajectory class:
class Trajectory: Shape{public:Trajectory(Shape *to);Trajectory();void check_collision_ball_to_all(Shape *b_tbc);void center_cpu_paddle_colbox(&CpuPaddle); //heres where the error is//I have also tried changing to PaddleC *cpupaddle//I have also tried" void center_cpu_paddle_colbox(PaddleC& CpuPaddle)";};
heres the PaddleC class:
class PaddleC:Shape {public:PaddleC();float getX();float getY();Trajectory getTraj();handle_events();applyVelocity();Trajectory moved;void setSpeed(float);float getSpeed(void);float getWidth(void);};
heres main:
main {Trajectory theTraj;PaddleP PlayPaddle;PaddleC CpuPaddle;Ball GameBall; CpuPaddle.applyVelocity(); theTraj.center_cpu_paddle_colbox(&CpuPaddle);}
thanks
David
Do you want public or private inheritance from the Shape class?
I think the default is private so the line
class Trajectory: Shape{
is equivalent to
class Trajectory: private Shape{
I couldnt quite figure out if private inheritance is indeed what you want by looking at the code, but if not, change it to
class Trajectory: public Shape{
The same with the other classes inheriting from Shape
I think the default is private so the line
class Trajectory: Shape{
is equivalent to
class Trajectory: private Shape{
I couldnt quite figure out if private inheritance is indeed what you want by looking at the code, but if not, change it to
class Trajectory: public Shape{
The same with the other classes inheriting from Shape
ok I have changed to public shape as suggested , I think the problem is to do with the order my classes are in; in my file:(I have tried using includes but I get the same problem ......because PaddleC needs to have a referrence to trajectory but trajectory also needs to deal with paddlec paddlep and ball objects passed in to handle collision detection
heres the structure as written in the file:
Class Timer{}
class Shape()
Class Trajectory: public shape {} (if I put paddleP above this it may fix the problem but then I get a problem with tha trajectory reference in the paddlep class)
Class PaddleP: public shape{}
class Frame {}
class Draw public Frame{}
Class PaddleC: public shape{}
class Ball: public Shape {}
main
^^the above is the exact order everythings in the one file , I have tried the classes in includes using ifndef etc but I still get the same problem ......its like Im in include hell lol . I just want to be able to pass objects to other classes. Unless there is something wrong with the way Im passing the PaddleC object? And why was I able to Pass a Shape *ashape object to the same class with no error?
heres the structure as written in the file:
Class Timer{}
class Shape()
Class Trajectory: public shape {} (if I put paddleP above this it may fix the problem but then I get a problem with tha trajectory reference in the paddlep class)
Class PaddleP: public shape{}
class Frame {}
class Draw public Frame{}
Class PaddleC: public shape{}
class Ball: public Shape {}
main
^^the above is the exact order everythings in the one file , I have tried the classes in includes using ifndef etc but I still get the same problem ......its like Im in include hell lol . I just want to be able to pass objects to other classes. Unless there is something wrong with the way Im passing the PaddleC object? And why was I able to Pass a Shape *ashape object to the same class with no error?
I have
Trajectory getTraj(); method prototype in the class PaddleP and PaddleC which is why I cant move PaddleC and P above trajectory in the file to fix this, even with includes its like Im in this vicous circle where both classes need to know of each other but only 1 knows about the other and not vica versa if you get what Im saying. How would you folks get around this?
Trajectory getTraj(); method prototype in the class PaddleP and PaddleC which is why I cant move PaddleC and P above trajectory in the file to fix this, even with includes its like Im in this vicous circle where both classes need to know of each other but only 1 knows about the other and not vica versa if you get what Im saying. How would you folks get around this?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement