Reputation: 1
First timer here i hope i dont bum you out with what is possibly a simple solution. still pretty new to c++. I will first just give you the code and we can go from there.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <list>
#include <algorithm>
using namespace std ;
class Point // class point will have an x and a y value
{
public:
// should contain attributes of a single point
double x ; // x value
double y ; //y value
double f ; //importance factor
//constructor
Point (double xpoint, double ypoint)// can accept two agruments
{
x = xpoint ;
y = ypoint ;
}
//default constructor
Point ()
{
x = 7;
y = 8;
}
//set methods
void setX (double pointx)
{
x = pointx;
}
void setY (double pointy)
{
y = pointy ;
}
void setF(double inF)
{
f = inF ;
}
//get methods for retyening values to main
double getX()
{
return x; // return the X form the setX
}
double getY ()
{
return y ; // return the y from the setY
}
//return the importance factor
double getF ()
{
return f ;
}
};
//global fucntion that takes three objects as arguments
static void calculateF (Point p1, Point p2, Point p3)// not sure if this syntax for objects as arguments is correct?.. seems to work ok
{
double F ; // i now have an f in the class and in the method? do i need both?
double xPoint1 = p1.getX(); // x value of object p1
double yPoint1 = p1.getY(); // y value of object p1
double xPoint2 = p2.getX(); // x value of object p2
double yPoint2 = p2.getY(); // y coordinates of object p2
double xPoint3 = p3.getX(); // x coordinates of obejct p3
double yPoint3 = p3.getY(); // y coorinates of obejct p3
//equation for working out f from these six values
//temp variables to store the length of the triangles sides.
double p2p3 = sqrt (((xPoint2 - xPoint3)*(xPoint2 - xPoint3)) + ((yPoint2 - yPoint3)*(yPoint2 - yPoint3))); //length point2 to point1 (PR in assingment notes)
cout << "p1p2 is = " << p2p3 << endl; // print the value of p2p3 (PR)
double p1p2 = sqrt (((xPoint1 - xPoint2)*(xPoint1 - xPoint2)) + ((yPoint1 - yPoint2)*(yPoint1 - yPoint2))); //length point1 to point 2 (LP in assingment notes)
cout << "p1p2 is = " << p1p2 << endl;
double p1p3 = sqrt (((xPoint1 - xPoint3)*(xPoint1 - xPoint3)) + ((yPoint1 - yPoint3)*(yPoint1 - yPoint3)));//length of point1 to point 3 (LR in assigment notes)
cout << "hypotenuse p1p3 is = " << p1p3 << endl;
F = p1p2 + p2p3 - p1p3 ; //equation for f
cout << "importance factor is " << F << endl ;
p2.setF(F); // unsure what this is meant to do??? setting F to f in class, not working
}
int main ()
{
// create objects of type Point, passing different x and y values to the contructor
Point point1 (7,5); // new point object point1 with value x=2.5, y=5.3
Point point2 (4,8); // second point obejct
Point point3 (8,9); // third point object
//then call calculateF fucntion
calculateF(point1, point2, point3);
cout << "point 2 has f value of: " << point2.getF() << endl ; // returning a random number ie not f
system ("PAUSE");
return 0 ;
}
the objects i am creating are points of a shapes boundary which have an x and y coordinates and also an importance factor which is calculated using some trig and vector maths (in calculateF).
I need the f value of the class Point to return to main after i have set it using the setF fucntion called it from the global fucntion caclculateF. Now the F value in the calculateF method is being caluculated correctly and being displayed accordingly. However when i call f using getF from main it just displays a random number (perhaps a memory address?) So something must be wrong with my class set up, my set or get methods for f. But as they are the same as the get and set methods for x and y, i cant see why its not returning the correct value in main. Go ahead and copy it and run it yourself.
Any and all help regarding this troubling matter would be greatly appreciated Many Thanks
Upvotes: 0
Views: 125
Reputation: 6570
You're passing the Point objects by value, which means that copies of the objects get passed to the calculateF
method, and any changes to that copy will only be seen locally in the calculateF
method.
Change the signature of calculateF
to accept a reference to a Point for the point2 parameter:
static void calculateF(Point p1, Point& p2, Point p3)
{
...
}
Upvotes: 1