tags:

views:

161

answers:

4
#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;
}
+1  A: 

One thing is that the line

cout << Line.linelength();

Should be

cout << ours.length();
epatel
Should be `ours.length()`; `linelength` is a local variable, not the name of the function.
Ben Voigt
+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
+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
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
That's because `Point` has no default constructor.
Roddy
Oh. Thanks. Didn't know it needed one.
Brian Hooper
@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
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