**UPDATE: FIXED* Higerground / anyone else --> PLEASE help me (C++)!

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
OK:

class rect
{
private:
float xcoord, ycoord, radius, mass, dxstep, dystep;

public:
rect()
{
xcoord = 0.0;
ycoord = 0.0;
radius = 0.0;
mass = 0.0;
dxstep = 0.0;
dystep = 0.0;
}

rect(float x, float y, float r)
{
xcoord = x;
ycoord = y;
radius = r;
mass = r * r;
dxstep = 0.0;
dystep = 0.0;
}

<< insert all of the standard get &amp; set routines here! >>

void function1(rect r)
{
function2(r)
}

void function2(rect r)
{
....
}

I call function1 from main (rectangle1.function1(rectangle2) and then in function1 I can modify both:

the private members of rectangle1 (xcoord, ycoord etc)

and

the member functions of rectangle2 (getx, gety, sety etc)

When it passes rectangle2 to function2 does this change? Because is sure seems to!

Cheers, Seb
 

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
This is driving me mad. It seems that in function2 I can still change xcoord, ycoord etc, but are they now of rectangle2 instead of rectangle1?

rectangle2.getx, rectangle2.sety, etc no longer seem to do anything if called from function2.

What's going on?

Seb
 

loup garou

Lifer
Feb 17, 2000
35,132
1
81
hmmm...If you're still looking for help after May, I'll be done with my C++ course ;)

Until then, good luck!
 

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
This is function2:

void processcollision(int type, rect r)
{
float tmp;

switch (type)
{
case 0:
case 1:
tmp = dxstep;
dxstep = r.getdx();
r.setdx(tmp);
break;
case 2:
case 3:
tmp = dystep;
dystep = r.getdy();
r.setdy(tmp);
break;
}
}


It was called from function1 (void collision(rect r)) as rectangle1.collision(rectangle2)

and it seems that the dxstep &amp; dystep values do actually change whereas the r.getdx() and r.setdy() etc don't do anything.

Seb

 

br0wn

Senior member
Jun 22, 2000
572
0
0
how about if u upload your code somewhere ?

Its hard to identify problems if you only give us
part of the code.

Answering one of your question :
why r.setdy() doesnt work <= because r is a copy
of the object (is passed by value into the function2).
To fix it, try pass by reference or pass a pointer
of the object (I hope you know how to do this,
you are a fully certified MS C++ programmer right ? ;) )
 

eyor

Banned
Feb 7, 2000
1,641
0
0
you may be passing by value rather than reference. slap an amphersand(&amp;) on and try again.
 

eyor

Banned
Feb 7, 2000
1,641
0
0
hmmmm.... why the smilie?
[edit] okay, it seems that putting a &quot;)&quot; after a &quot;&amp;&quot; makes a smilie but leaves the amphersand. &amp;)
 

Killbat

Diamond Member
Jan 9, 2000
6,641
1
0
Fun Visual C++ project: :) (yes, I did this during class today. It's quite effective.:D)

#include <stdlib.h>
void main()
{
int *loc;
for (loc;;loc++)
{
*loc=rand();
}
}

What does it do? Duh. It crashes Windows. With a BANG! :D
 

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
Ah, passing by value am I . . .

brown, I hope you don't really think I'm a fully certified MS programmer, that was a joke . . .

Righto, I suppose I better learn the evil that is pointers then.

Seb
 

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
OK thanx everyone. I know I still have a lot to learn!

just doing void function2(rect &amp;r) solved my problem instantly. Doh.

Seb
 

Killbat

Diamond Member
Jan 9, 2000
6,641
1
0
NOTICE: The program mentioned above will only blow up Windows 95/98. No way will 2000 let you do that. :D

How about those pointers, eh sebfrost? I can forsee pointers being my main source of bugs as I learn C++. :)
 

downhiller80

Platinum Member
Apr 13, 2000
2,353
0
0
I know nothing about pointers, and whenever I try to understand them all the &amp;'s and *'s confuse the hell out of me!

I'll try my Deitel book and see if they teach it well.

Seb