I have the code:
class Point3D{
protected:
float x;
float y;
float z;
public:
Point3D(){x=0; y=0; z=0;}
Point3D(const Point3D & point){x = point.x; y = point.y; z = point.z;}
Point3D(float _x,float _y,float _z){x = _x; y = _y; z = _z;}
}
class Vector3D{
protected:
Point3D start;
Point3D end;
public:
...
Point3D getSizes(){
return Point3D(end-start);
}
}
I want to create and operator+ for Point3D that will take an vector:
Point3D & operator+(const Vector3D &vector){
Point3D temp;
temp.x = x + vector.getSizes().x;
temp.y = y + vector.getSizes().y;
temp.z = z + vector.getSizes().z;
return temp;
}
But when I put that operation iside Point3D class declaration, I got error because I don’t have Vector3D declared here. And I cannot move Vector3D declaration before Point3D, because it uses Point3D.
You can solve this by moving the function definition after the definition of
Vector3D, and just declare the function in the class definition. This requires a declaration ofVector3D, but not the full definition.Also, never return a reference to a local automatic variable.