forgot the &?

I haven't seen this error before:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//header

#include <iostream>
using namespace std;

#ifndef XYZ_H
#define XYZ_H

class XYZPoint
{
public:
	XYZPoint( ); //Default Constructor
	XYZPoint( float newX, float newY, float newZ ); //Constructor
	
	void Distance( XYZPoint n );
	
	float x;
	float y;
	float z;
};

#endif //XYZ_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//.cpp
#include <iostream>
#include "xyz.h"
#include <cmath>
using namespace std;

XYZPoint::XYZPoint( float newX, float newY, float newZ ) //constructor
{
  	x = newX;
  	y = newY;
  	z = newZ;
}

XYZPoint::XYZPoint ( ) //default constructor
{
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

void XYZPoint::Distance( XYZPoint n)
{

XYZPoint temp ( 0, 0, 0 );

temp.Distance=sqrt((n.x*n.x) + (n.y*n.y) + (n.z*n.z));

return ;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//main
#include <iostream>
#include "xyz.h"
using namespace std;

int main()
{
cout << "Hello world." << endl;
/*
XYZPoint P0( 0, 0, 0 );
XYZPoint P1( 5, 3, 1 );
XYZPoint P2( -3, 7, 1);

P0.Distance(P1);*/

return 1;
}


xyz.cpp: In member function ā€˜void XYZPoint::Distance(XYZPoint)ā€™:
xyz.cpp:35: error: invalid use of member (did you forget the ā€˜&ā€™ ?)


I've read that it's because I'm not using the proper function call syntax. I'm not trying to call a function, I'm trying to use the method XYZPoint::Distance?
Line 26, you writing temp.Distance, which appears to be an attempt to call the method Distance(), but you try to assign some value to it, which makes no sense.
I thought I needed to define temp somehow? When I take that part out it says there is no match for operator =. I just want to use the default = operator...
Last edited on
temp is defined on line 24; you construct it with all 0s.
but if I make it
1
2

Distance=sqrt((n.x*n.x) + (n.y*n.y) + (n.z*n.z));


It says the same forgot the & error.
You can't assign to Distance; Distance is a method, and can only be called like a function.
hmmm... I work more.
Closer. In full disclosure, this is an assignment, and I only have about 20 mins left to turn it in >.<


" Create a class called XYZPoint that has public floats to hold
the x, y, and z values of a 3D point. The class should have a constructor
with default values (0,0,0) and a method called Distance( XYZPoint )
that returns the Euclidean distance between the argument and the point
the method is called on. Also override the stream insertion operator so
that users can print the point to the screen in the format [x, y, z].
Test your program with the points [5 3 1] and [-3 7 1]. Turn in copies
of all three files needed to compile your
program: main.cpp, XYZPoint.cpp, and XYZPoint.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//header
#ifndef XYZ_H
#define XYZ_H

class XYZPoint
{
public:
	XYZPoint( ); //Default Constructor
	XYZPoint( float newX, float newY, float newZ ); //Constructor
	
	friend ostream& operator<<(ostream& stream, const XYZPoint& P );
	void Distance( XYZPoint n );
	
	float x;
	float y;
	float z;
};

#endif //XYZ_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//.cpp
#include <iostream>
#include "xyz.h"
#include <cmath>
using namespace std;

XYZPoint::XYZPoint( float newX, float newY, float newZ ) //constructor
{
  	x = newX;
  	y = newY;
  	z = newZ;
}

XYZPoint::XYZPoint ( ) //default constructor
{
	x = 0.0;
	y = 0.0;
	z = 0.0;
}


void XYZPoint::Distance( XYZPoint n) 
{

XYZPoint temp ( 0, 0, 0 );

temp.x = sqrt(n.x*n.x);
temp.y = sqrt(n.y*n.y);
temp.z = sqrt(n.z*n.z);
cout << temp.x << " " << temp.y << " " << temp.z << endl;

return ;
}

ostream& operator<<( ostream& stream, const XYZPoint& n )
{
  stream << "[ " << n.x << " " << n.y << " " << n.z << " ]" << endl;

  return stream;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//main
#include <iostream>
#include "xyz.h"
using namespace std;

int main()
{
//cout << "Hello world." << endl;

XYZPoint P0( 0, 0, 0 );
XYZPoint P1( 5, 3, 1 );
XYZPoint P2( -3, 7, 1);

P1.Distance(P2);

return 1;
}


It will calculate now, but I haven't finished the distance part. I also started working out the output. It will all compile, but it won't put out in the format I want. Just like my women.
Times up! I'll get a chance to correct it, but that sucks. Anyways, I can't really think of a way to subtract P1 from P2 using the temps in the Distance method. The temps gets destroyed when the method exits. I wrote a different program for computing complex numbers and P1.Distance(P2); did the trick. I'm not really sure where to go from here.
This may not be obvious, but it's impossible to find the distance between two points while only using information from one of the points. There is no need for the temp variable, and Distance should likely return a value.. maybe the distance (which wouldn't be in the form of an XYZPoint.)
Cire, you are so good at sarcasm it even comes across in text!

I know I need to find the distance between two points, not the distance from one point in space to itself. Again, when comparing two complex numbers in my previous program, P1.Distance(P2) worked. Not exactly that, but something similar. I'm learning, and trying to figure this out. If I already knew how to do what I wanted, I would have already turned in the assignment and continued on with drinking and beating off, don't you think?

You are also correct about Distance returning a value. That's what I want too! For now though, I'd really like it to compile so I can start generating output and seeing what it does. I'm really more of a trial and error guy, instead of writing blocks of code and it working the first time kind of guy.

And I have gotten it to compile. And I can perform operations on any X, Y, and Z I send to distance. Now I need to figure out how to send it TWO X, Y and Z's. Then I can work on having it use the overridden operator<< and having it output what I want.
Last edited on
You already have two x's, two y's and two z's. You have that of the object the method is invoked on, and that of the object specified by the parameter.

1
2
3
4
5
6
7
8
double XYZPoint::Distance( XYZPoint other) const
{
    double x_diff = x - other.x ;
    double y_diff = y - other.y ;
    double z_diff = z - other.z ;

    return sqrt( x_diff*x_diff + y_diff*y_diff + z_diff*z_diff ) ;
}

Interesting. I chose
1
2
3
4
5
6
7
8
9
10
11
12
13
XYZPoint XYZPoint::Distance( XYZPoint n, XYZPoint b) 
{
float Dx = 0.0;
float Dy = 0.0;
float Dz = 0.0;

Dx = sqrt(n.x*n.x+b.x*b.x);
Dy = sqrt(n.y*n.y+b.y*b.y);
Dz = sqrt(n.z*n.z+b.z*b.z);
XYZPoint PA(Dx,Dy,Dz);

return PA;
}


I'll try yours too.
Got it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//header

#include <iostream>
using namespace std;

#ifndef XYZ_H
#define XYZ_H

class XYZPoint
{
public:
	XYZPoint( ); //Default Constructor
	XYZPoint( float newX, float newY, float newZ ); //Constructor
	
	friend ostream& operator<<(ostream& stream, const XYZPoint& P );
	XYZPoint Distance( XYZPoint n, XYZPoint b );
	
	float x;
	float y;
	float z;
};

#endif //XYZ_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//.cpp
#include <iostream>
#include "xyz.h"
#include <cmath>
using namespace std;

XYZPoint::XYZPoint( float newX, float newY, float newZ ) //constructor
{
  	x = newX;
  	y = newY;
  	z = newZ;
}

XYZPoint::XYZPoint ( ) //default constructor
{
	x = 0.0;
	y = 0.0;
	z = 0.0;
}


XYZPoint XYZPoint::Distance( XYZPoint n, XYZPoint b) 
{
float Dx = 0.0;
float Dy = 0.0;
float Dz = 0.0;

Dx = sqrt((n.x-b.x)*(n.x-b.x));
Dy = sqrt((n.y-b.y)*(n.y-b.y));
Dz = sqrt((n.z-b.z)*(n.z-b.z));


XYZPoint PA(Dx,Dy,Dz);

return PA;
}

ostream& operator<<( ostream& stream, const XYZPoint& n )
{
  stream << "[ " << n.x << " " << n.y << " " << n.z << " ]" << endl;

  return stream;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main
#include <iostream>
#include "xyz.h"
using namespace std;

int main()
{
//cout << "Hello world." << endl;

XYZPoint P0( 0, 0, 0 );
XYZPoint P1( 5, 3, 1 );
XYZPoint P2( -3, 7, 1);

cout << (P0.Distance(P1, P2)) << endl;
return 1;
}

[ 8 4 0 ]
That's great.

Except a point isn't a distance. Especially a point which seems to be constructed from some random, arbitrary formula.

And P0.Distance() doesn't actually do anything related to P0.
Topic archived. No new replies allowed.