Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 'Riddle Me This', passing objects into class functions...

Author
Message
Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 16:02 Edited at: 7th Jul 2008 16:33
Am I right here, in thinking that, if you wanna pass, objects into class functions, you use send it via 'MyClass', and receive it via '&PassedClass'.

Deus->Create_Object ( Player, 1000, 600 ) ;

In the above line, 'Player' is an object of the class 'Object' and is sent into the function 'Create Object' of the Deus object, of class 'Maestro' via the following line:

void Create_Object ( Object &Obj, int PosX, int PosY ) ;

and...

void Create_Object ( Object &Obj, int PosX, int PosY )
{

}

Which according to two books and several websites I have read should not return the statement:

"Cannot convert parameter 1 from 'Object *' to 'Object &'"

But that is what I is getting.

Why?

EDIT:

Alright, how about this one:

Movement *Mover ;
Object *Player ;

Mover = new(Movement) ;
Object *Player ;

Mover->Collision( Player ) ;
void Movement::Collision( Object *Player )

Works fine but...

Maestro *Deus ;
Object *Player ;

Deus = new(Maestro) ;
Player = new(Object) ;

Deus->Create_Object ( Player, 1000, 600 ) ;
void Create_Object ( Object *Player, int PosX, int PosY ) ;

Returns:

1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Maestro::Create_Object(class Object *,int,int)" (?Create_Object@Maestro@@QAEXPAVObject@@HH@Z) referenced in function "void __cdecl DarkGDK(void)" (?DarkGDK@@YAXXZ)

Anyone care to have a guess at why the f*** one works but something else with a different name don't?

EDIT:

Ok my bad. At some point I removed:

Maestro::

From the start of the class functions, hence the error. It will probably work now.

We are here and it is now.
FERSIS
18
Years of Service
User Offline
Joined: 17th May 2006
Location:
Posted: 7th Jul 2008 16:35
Cool avatar tough
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 7th Jul 2008 16:38 Edited at: 7th Jul 2008 16:38
Judging by the error "Cannot convert parameter 1 from 'Object *' to 'Object &" your 'Player' must be defined as an address to 'Object', which would explain the error, i.e. you have 'Object* Player = new Object' or something similar, to fix this you need to pass the reference to the function and not the pointer, this can be done by passing '*Player'.

Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 17:44 Edited at: 7th Jul 2008 17:52
Player is defined as:



Which means it is a pointer to start with so:



Sends the addy of Player into the Deus functions which is why:



Shows me the updated contents of Player post Deus function calls within the following IF statements:



Which runs the setting code only once.

As far as I can tell this can't be sending a copy because the object is permanently updated. Can't be sure if I'm sending the object itself or not but as Player is created as a pointer and received as an Addy reference by Deus I don't see how it can be. Additionally the Deus functions are Voids so they don't return anything so if I was sending the object it would not be updated as the object is not returned from Deus. It's address is sent, the object is modded (not returned) and then after the Deus functions have been called, the main accesses the object again and has clearly been updated. If it hadn't been the program would have either shown rubbish or crashed as the variables of the class have not been updated since creation.

It's working fine now anyway since I changed:



Within Deus (the Maestro class) to:





We are here and it is now.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 7th Jul 2008 17:52
A much easier method of passing-by-reference is this:

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 7th Jul 2008 17:55
That's passing the address and not the reference.

Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 17:55
Yes that would work, except my class objects are being created as pointers.



And i get the error:



We are here and it is now.
Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 17:56
So whats the difference dark coder becuase it seems to work as is?

We are here and it is now.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 7th Jul 2008 18:14 Edited at: 7th Jul 2008 18:15
I would think create the object in the regular fashion and passing the address to the function like I posted will be efficient and clearer code.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 7th Jul 2008 18:57
Quote: "So whats the difference dark coder becuase it seems to work as is?"


There's nothing wrong with your new code, I was initially only talking about your pre-edit reference usage, as you were passing the address to a function that expected the reference. It's fine now.

Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 19:11
Ah, ic Dark. Still, I don't know the difference between an addy and a ref.

Well Mahoney I picked up creating pointers to objects instead of objects themselves while learning DirectX so 'If it works, do it' is my motto unless I see evidence to a better contrary.

We are here and it is now.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 7th Jul 2008 19:14 Edited at: 7th Jul 2008 19:14
I just figured that create the objects as just that, objects, then passing the address to a function wanting a pointer would be neater and more readable. I may be wrong, though. Just a suggestion.
Ludo
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: UK, Oxfordshire
Posted: 7th Jul 2008 19:57
*Shurg* Couldn't say dude.

We are here and it is now.
Jaheco
16
Years of Service
User Offline
Joined: 30th Jun 2008
Location:
Posted: 7th Jul 2008 20:05
I myself just typically do things like:



There is a chance that I am completely off the mark and this post won't help you at all, 'cause I'm not entirely sure what you are trying to do. I just figure if your compiler is complaining about converting from one type to the other, and you don't entirely need the second type to begin with, it might be better not to do any converting at all.

Login to post a reply

Server time is: 2024-09-30 01:40:29
Your offset time is: 2024-09-30 01:40:29