Point to double?

Hi. I have this long code and it has a class and inside the class there is a private Point called p1. I am trying to set a double type x to this Point. How do I do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
 class Triangle
{
private:
	Point p1;
public:
        void setPart(const double x);
};

void Triangle::setPart(const double x) 
{
  ????
}

Thanks..!!
p1.x = x
Um... If you mean you are trying to store x (the parameter) into p1 (your member variable), the syntax would be just a normal assignment operator:
1
2
3
4
void Triangle::setPart(const double x) 
{
    p1 = x; //store x into p1
}

However, p1 is a Point, and x is a double, so I have no way of knowing if that even makes sense; (we would need to see the point class.) For exmaple, if Point represented a point in standard 2D (euclidean) space, then it has two components (x,y). It wouldn't be just one number; it would be two. And so it would be up to the class to explicitly define what assigning it a double value would do:
1
2
3
4
5
6
7
//p1 == (4,6)
p1 = x
//now p1 == (x,6)
// or p1 == (4,x)
// or p1 == (x,x)
// or p1 == (x,0)
// or ERROR, undefined operation...? 
I forgot to add the Point class here it is. I am trying to access the double px in the Point class through Triangle.
1
2
3
4
5
6
7
8
9

class Point
{
private:
	double px;
public: 
        void setX(const double x);
        double getX() const;
};
Last edited on
You can't, because px is private.

You do have a nice, handy setX() interface method, though.
What if I did this? What I am getting access to?

1
2
3
4
5

void Triangle::setPart(const double x) 
{
  p1.getX() ; //where getX return px..!!
}

If it's been implemented sensibly, it should return the value of px, just like your comment says.
Looks like you should do p1.setX(x) in your setPart() function to set a double type x to this Point
I am trying to use it in another triangle function.

1
2
3
4
5
6
Point Triangle::getT() const
{
  double t(0.0);
  t = px + length; 
  return(t);
}


Am I using correctly or do I have to use p1.getX() instead of using px directly?
do I have to use p1.getX() instead of using px directly?

Yes. You can't access px, because it's private, so you have to use the accessor method instead.
Last edited on
One more question, when I did return(t) I get this error: error: conversion from ‘double’ to non-scalar type ‘Point’ requested.
Since I am calling Point Triangle, how do I return the value?
You can return objects just like primitive variables.

Edit: But are you sure it's a Point that you want to return? The name of your method sounds like its purpose is to obtain the value of t. And in the code you've posted, t is a double, not a Point.
Last edited on
I do want to return a value but problem is I can't use double there because the function has non-scalar type Point..!!
Well, that's your decision to make, isn't it? What do you want it to return? What is your method supposed to be doing?
What basically I am trying to do is I want to get the value px and add length to it in the Point Triangle :: getT() function. It won't let me return an int or a double so I have to declare a Point but I still don't quiet get the result I want.

1
2
3
4
5
6
7
8

Point Triangle::getT() const
{
  Point t;
  t = p1.getx() + length; 
  return(t);
}
Last edited on
You're still not being clear about what you want your method to do. What do you want it to return? Why do you want it to return anything at all? What do you want to do with the thing it's returning? What is the mysterious "T" that it's getting supposed to represent?
Last edited on
Basically I have x and y coordinates and I want to get the top left vertex by adding the value of px to the length using this function.
OK, so it's supposed to be a Point, then. So, in your method, construct a Point object, set the values in it that you want, and then return it like you would any other variable.

Why give your function such a cryptic name? A well-named method should make it clear what it does, to you and anyone else who is looking at your code. Why not call it GetTopLeftVertex?

You are right I should have made it clear to you and I will change it to what you suggested.
Topic archived. No new replies allowed.