#include <iostream>
#include <math.h>
using namespace std;
class Point{
public:
Point(int xx, int yy);
~Point();
int getX();
int getY();
void setX(int xx){ x = xx; }
void setY(int yy){ y = yy;}
private:
int x;
int y;
};
Point::Point(int xx, int yy)
{
x = xx;
y = yy;
}
Point::~Point()
{
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
class Line
{
public:
Line(Point one, Point two);
~Line();
float length();
private:
Point a;
Point b;
};
Line::Line(Point one, Point two)
{
a.setX = one.getX;
a.setY = one.getY;
b.setX = two.getX;
b.setY = two.getY;
}
float Line::length()
{
int x1,y1,x2,y2;
float linelength;
x1 = a.getX;
y1 = a.getY;
x2 = b.getX;
y2 = b.getY;
linelength = ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1));
linelength = sqrtf(linelength);
return linelength;
}
int main()
{
Point mine(1,1);
Point yours(2,2);
Line ours(mine, yours);
cout << Line.linelength();
return 0;
}
views:
161answers:
4
+1
A:
One thing is that the line
cout << Line.linelength();
Should be
cout << ours.length();
epatel
2010-07-19 13:31:47
Should be `ours.length()`; `linelength` is a local variable, not the name of the function.
Ben Voigt
2010-07-19 13:35:36
+2
A:
Your question isn't clear. You mean it doesn't compile?
try calling functions properly ie
x1 = a.getX();
instead of
x1 = a.getX;
and
a.setX(one.getX())
;
instead of
a.setX = one.getX;
but there is probably more wrong...
Ben Schwehn
2010-07-19 13:33:14
+2
A:
a.setX = one.getX;
a.setY = one.getY;
b.setX = two.getX;
b.setY = two.getY;
setX
and getX
are functions, not variables. Therefore, you need to use them as such:
a.setX(one.getX());
In the future for getting help on here, you should give a bit more information on what is wrong with your code: what compiler errors are you getting? what is not working? what are you having issues with?
orangeoctopus
2010-07-19 13:33:17
A:
I've been having a little play with this, and I was wondering why...
Line(const Point &one, const Point &two) : a(one), b(two) {};
is fine, but
Line(const Point &one, const Point &two);
then (later)
Line::Line(const Point &one, const Point &two)
{
a = one;
b = two;
}
gets...
frag.cpp: In constructor ‘Line::Line(const Point&, const Point&)’:
frag.cpp:57: error: no matching function for call to ‘Point::Point()’
frag.cpp:24: note: candidates are: Point::Point(int, int)
frag.cpp:8: note: Point::Point(const Point&)
frag.cpp:57: error: no matching function for call to ‘Point::Point()’
frag.cpp:24: note: candidates are: Point::Point(int, int)
frag.cpp:8: note: Point::Point(const Point&)
Anyone any idea why?
Brian Hooper
2010-07-19 13:59:35
@Brian, your first line of code `a(one), b(two)` explictly specifies the copy constructor in the initialization list. The second variant does not specify any constructor so the default will get used. If there's no default, you get the error. Because a Point() has constructors defined (copy, and XY) a default constructor is NOT automagically generated.
Roddy
2010-07-19 14:32:43
Thank you. I was trying to make sense of the disaster area posted at the top, and having got down to the last remaining problem, didn't understand. Thanks for your guidance.
Brian Hooper
2010-07-19 15:02:37