Line2D Undefined Reference

I am writing a program that sorts data. Point2D and Point3D has no problem reading data and storing it as an object. However, if I try to read in Line2D or Line3D, I get this error showing up twice:

undefined reference to `Point2D::Point2D()'|
undefined reference to `Point2D::Point2D()'|


Main Driver Program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
    string data="Line2D|[5, 7]=[3, 8]"
    istringstream iss(data);
    string dType, dVal1, dX1, dY1, dVal2, dX2, dY2;
    getline(iss, dType, '|');
    getline(iss, dVal1, '[');
    getline(iss, dX1, ',');
    getline(iss, dY1, ']');
    getline(iss, dVal2, '[');
    getline(iss, dX2, ',');
    getline(iss, dY2, ']');

    cout<<"("<<dX1<<", "<<dY1<<"), ("<<dX2<<", "<<dY2<<")";

    int x1 = stoi (dX1);
    int y1 = stoi (dY1);
    int x2 = stoi (dX2);
    int y2 = stoi (dY2);

    Point2D p2d1 (x1, y1) ;
    Point2D p2d2 (x2, y2);
    Line2D l2d (p2d1, p2d2);

//    cout<<l2d;
}


Point2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef Point2D_h
#define Point2D_h
#include <iostream>
#include <math.h>
using namespace std;

class Point2D
{
protected:
    int x;
    int y;
    double distFrOrigin;

    void setDistFrOrigin()
    {
        distFrOrigin=sqrt(pow(x,2)-pow(y,2));
    }

public:
    Point2D();

    Point2D(int x, int y)
    {
        this->x=x;
        this->y=y;
    }

    int getX()
    {
        return x;
    }

    int getY()
    {
        return y;
    }

    double getScalarValue()
    {
        return distFrOrigin;
    }

    void setX(int x)
    {
        this->x=x;
    }

    void setY(int y)
    {
        this->y=y;
    }

    friend ostream & operator << (ostream &os, Point2D &p2d)
    {
        cout<<"Point2D = ("<<p2d.x<<", "<<p2d.y<<")"<<endl;
    }
};
#endif // Point2D_h



Line2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef Line2D_h
#define Line2D_h
#include <iostream>
#include <cmath>
#include "Point2D.h"
using namespace std;

class Line2D
{
private:
    Point2D pt1;
    Point2D pt2;

protected:
    double length;

    void setLength()
    {
        length=sqrt(pow(pt1.getX()-pt2.getX(),2)+pow(pt1.getY()-pt2.getY(),2));
    }

public:
    Line2D();

    Line2D(Point2D pt1, Point2D pt2)
    {
        this->pt1=pt1;
        this->pt2=pt2;
    }

    Point2D getPt1()
    {
        return pt1;
    }

    Point2D getPt2()
    {
        return pt2;
    }

    double getScalarValue()
    {
        return length;
    }

    void setPt1(Point2D pt1)
    {
        this->pt1=pt1;
    }

    void setPt2 (Point2D pt2)
    {
        this->pt2=pt2;
    }

    friend ostream & operator << (ostream &os, Line2D &l2d)
    {
        int distance=l2d.getScalarValue();
        cout<<"Line2D = ("<<l2d.pt1.getX()<<", "<<l2d.pt1.getY()<<"), "<<l2d.pt2.getX()<<", "<<l2d.pt2.getY()<<"); DISTANCE = "<<distance<<endl;
    }
};
#endif // Line2D_h 


Did I do something wrong here?
Where do you define the Point2D::Point2D() constructor?

You have the same potential issue for Line2D.
You declare the signature for a 0-arg constructor, but don't implement it.

If you used member initialization lists, you could actually get away with not letting a default constructor be defined at all.

1
2
Point2D(int x, int y)
: x(x), y(y) { } 


1
2
Line2D(Point2D pt1, Point2D pt2)
: pt1(pt2), pt2(pt2) { }


This properly initializes pt1 and pt2 instead of default initializing them and then re-assigning them in the constructor.
Last edited on
Topic archived. No new replies allowed.