need to pass a C++ object to another class

Started by
24 comments, last by redwingdw 17 years, 8 months ago
There's an article that shows how to separate C++ files, but I can never find it and never remember to save a link to it when it (frequently) gets posted. Hopefully someone will help out there. In the meantime:
// Timer.hclass Timer{	public:		Timer();		void member_function();		// constructor, destructor, member function declarations	private:		int member_variable;		// member variables};// Timer.cpp#include "Timer.h"Timer::Timer(){	// implementation}void Timer::member_function(){	// implementation}// etc.// Shape.hclass Shape{	public:		Shape();		std::vector<SDL_Rect>colBox_ball;		std::vector<SDL_Rect>colBox_ppaddle;		std::vector<SDL_Rect>colBox_cpaddle;		// etc.};// Shape.cpp#include "Shape.h"Shape::Shape(){	// implementation}// etc.// Tradjectory.h#include "Shape.h"// forward declarationclass PaddleC;class Tradjectory	:	public Shape{	public:		Trajectory(Shape * to);		void center_cpu_paddle_colbox(PaddleC & CpuPaddle);		// etc.};// Tradjectory.cpp#include "Tradjectory.h"#include "PaddleC.h"Tradjectory::Tradjectory(Shape * to){	// implementation}void Tradjectory::center_cpu_paddle_colbox(PaddleC & CpuPaddle){	// implementation}// etc.// PaddleC.hclass PaddleC	:	public Shape{	public:		// etc.};// PaddleC.cpp#include "PaddleC.h"// etc.// etc. // main.cpp#include "Tradjectory.h"#include "PaddleP.h"#include "PaddleC.h"#include "Ball.h"int main(){	Trajectory theTraj;	PaddleP PlayPaddle;	PaddleC CpuPaddle;	Ball GameBall;	CpuPaddle.applyVelocity();	theTraj.center_cpu_paddle_colbox(CpuPaddle);}

Each class is separated into a header file and an implementation file. Header files contain class defintions. Implementation files contain member function definitions. Each header file #includes the header file for any class its class inherits from or contains a member of. Each header file forward declares any class it mentions as a parameter but does not use. Each implementation file #includes its header file plus the header file of any class it uses.

Σnigma
Advertisement
Thanks enigma, I MAY be getting somewhere using include files but what a knightmare to get the order right at this point! ....I am using
#ifndef HEADERFILENAME_H#define HEADERFILENAME_Hclass definition goes here#endif


Actually what you said about immplemention files when using includes ......may have solved another problem, you see my OO experience is from java and I was putting the class definition and immplementation in the same file ......I am getting when calling the class constructor from main a not a class or namespace error ....my class definitions arent erroring at this point so Im hoping that means if I put immplementation files in everything will work but I will check that out later (Im going for a break :-) )

Thanks as always for the help, I know im asking alot but Im getting an solving quite a few problems on a daily basis so I am making ground, whats ironic is my program maths and collision boxes worked as intended thus far but it was my program structure thats came back to haunt me. A person on this forum already advised me to use includes and include gaurds but I didnt want to become bogged down with technicalities when I was determined to actually follow 1 project through ). It seems I am going to have to get used to includes though hehe, theres know way around it that I can see when trying to do a game pure OO(Well Ive no doubt it can be done :-) but not from me at this point)

David

Have got from 100 odd errors down to 1 error by studying what was said I am now getting the following error:

c:\mycgames\sdl_cprojs\pongo\pjmain.cpp(275) : error C2664: 'center_cpu_paddle_colbox' : cannot convert parameter 1 from 'class PaddleC *' to 'class PaddleC &'

heres my setupmain (PaddleC CpuPaddle;theTraj.center_cpu_paddle_colbox(&CpuPaddle);}

then in trajectory:
#include "Shape.h"class PaddleC;class Trajectory: public Shape{public:float speed;Trajectory();void center_cpu_paddle_colbox(PaddleC & CpuPaddle);};

now in Trajectory.cpp
#include "Trajectory.h"#include "PaddleC.h"void Trajectory::center_cpu_paddle_colbox(PaddleC & CpuPaddle) {}


I think Im nearly there, I dont get how this is syntactically incorrect
This error message is something deeper than the includes now i think, heres the rest of the error

A reference that is not to 'const' cannot be bound to a non-lvalue

headache material lol
How are you calling the function? And will the function modify the passed-in object? Since it's passed by reference, and the function doesn't *promise* that it won't modify the passed-in object, you cannot pass a temporary value, because modifying a temporary value would be bad (there'd be no way to see the changes).

To make that promise, you do like it says, and prototype the function as 'void center_cpu_paddle_colbox(const PaddleC & CpuPaddle);'. Of course, having made that promise, the compiler will proceed to call you on it, by making sure that the function's code doesn't directly change CpuPaddle, or pass it to anyone else not making an appropriate promise.

Const correctness.

Just to update Ive looked at the latest error to do with const and lvalues and as a few people have put it .......its to cryptic , can someone show me a fix (still passing the object) that relates to my code. I know people will say its wrong for me to pass an object in this way when they look at the latest error but I want to be able to do it anyway.
ahh thanks and dam! ......hmm Im thinking here and of hand I just need various paddle values as the paddle moves......but I wont actually be changing the paddles members from trajectory. I will be using the paddles on each loop iteration with the ball object and altering the collision boxes as the paddlep, paddlec and ball move, the collision arrays are in trajectory. I know its not ideal :-).....but the trajectory object is done in main so I figure it will stay in scope and the arrays should be okay to update through it.......sorry vectors

thanks
God dam lol I must have tested my old way of doing it before going through enigmas guide :-) above, I am know getting 0 errors and dont need to set it as const I did

#include "Shape.h"class PaddleC;class Trajectory: public Shape{public:float speed;Trajectory();void center_cpu_paddle_colbox(PaddleC & CpuPaddle);};


Trajectory.cppvoid Trajectory::center_cpu_paddle_colbox(PaddleC  CpuPaddle) {}


thats the referrence fixed , Im back to include hell :-) but its not as bad as it was



Im getting there now but I have come up against another unique problem in regards to including ,

In my Ball class definition in the Ball.h file I want a Refference up where the variables get re assigned , a referrence to Trajectory (I may take this out but knowing how to do this is getting my knowledge up)

e.g
class Trajectory;
class Ball public shape {
Trajectory movements; //creates a default trajectory object to be gotten at a later time

}

Im getting a "moved undeclared identifier" error......once again Im probably getting my wires crossed with java as doing this wasnt a problem, I realise the above is not a null pointer as opposed to java which is the way I want it.

I have tried including Trajectory.h in ball.cpp, I believe I also tried to include Trajectory.h in ball.h but that didnt fix it. I know know howto do class forwarding roughly which has sorted alot but I need to know how to deal with objects in class definitions?

Thanks alot
David
I would go back to the the old center_cpu_paddle_colbox function:

void center_cpu_paddle_colbox( PaddleC & CpuPaddle );

Then I would change how you use it instead:

theTraj.center_cpu_paddle_colbox( CpuPaddle );

If the center_cpu_paddle_colbox function dont need write access to the CpuPaddle, the prefered way to declare the function would be:

void center_cpu_paddle_colbox( const PaddleC & CpuPaddle );

However, not doing so is not an error and will work just fine.

Also you can use forward declaration to solve the circular problem, as enigma suggested.

This topic is closed to new replies.

Advertisement