Template <class T>

Hey guys. Could anyone tell me why I am getting a ton of undefined reference to... errors with this code below.

thanks

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
#ifndef POINT_H
#define POINT_H
#include <iostream>

using namespace std;

template <class T>
class Point
{
  T x;
  T y;

  public:

  Point();
  Point (T X, T Y);
  ~Point();
  Point (const Point<T>& rhs);

  void reset_location(T X, T Y);
  T getX() {return x;}
  T getY() {return y;}

  Point<T>& operator =(const Point<T> &rhs);
  T& operator +(T delta);
  T operator -(Point<T> &rhs);

friend ostream& operator <<(ostream &os, const Point<T> &X);


};




#endif 


.cpp

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include "point.h"
#include <math.h>
#include <iostream>


template <class T>
Point<T>::Point() : x(0), y(0)
{

}

template <class T>
Point<T>::Point (T X, T Y) : x(X), y(Y)
{

}

template <class T>
Point<T>::~Point()
{

}

template <class T>
Point<T>::Point(const Point<T> &rhs)
{
    x = rhs.x;
    y = rhs.y;

}

template <class T>
void Point<T>::reset_location(T X, T Y)
{
    x = X;
    y = Y;

}

template <class T>
Point<T>& Point<T>::operator =(const Point<T> &rhs)
{
    x = rhs.x;
    y = rhs.y;

    return *this;

}

template <class T>
T& Point<T>::operator +(T delta)
{
    x = x + delta;
    y = y + delta;

    return Point(x,y);

}

template <class T>
T Point<T>::operator -(Point<T> &rhs)
{

    T newX = x - rhs.x;
    T newY = y - rhs.y;

    T Xsqrd = newX * newX;
    T Ysqrd = newY * newY;

    T sumXY = Xsqrd + Ysqrd;

    T dist = sqrt(sumXY);

    return dist;



}

template <class T>
ostream& operator <<(ostream &os, const Point<T> &X)
{
    os << "Your coordinates are: (" << X.x << "," << X.y << ")" << endl;

    return os;

}


main

[code]
#include "point.h"
#include <iostream>

using namespace std;


int main ()
{
Point<int> p;
Point<int> p1(2, 3);
Point<int> p2(3, 6);


cout << p << endl << p1 << endl << p2 << endl;

p = p1;

cout << p <<endl;

//p1 = p1 + 3;

//cout << "Addition to delta test:" << endl << p1 << endl;

int dist = p1 - p2;

cout << "Subtraction test:" << endl << dist << endl;


}
[\code]
closed account (DSLq5Di1)
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
Thanks that was a huge help.

when I try to use p1 = p1 +3. It's giving me a no match for operator =

What am I doing wrong?

Thanks again.

/home/greg/workspace/C Part II/finalQ2/main.cpp||In function ‘int main()’:|
/home/greg/workspace/C Part II/finalQ2/main.cpp|20|error: no match for ‘operator=’ in ‘p1 = p1.Point<T>::operator+ [with T = int](3)’|
/home/greg/workspace/C Part II/finalQ2/point.h|72|note: candidates are: Point<T>& Point<T>::operator=(const Point<T>&) [with T = int]|
/home/greg/workspace/C Part II/finalQ2/point.h||In member function ‘T& Point<T>::operator+(T) [with T = int]’:|
/home/greg/workspace/C Part II/finalQ2/main.cpp|20|instantiated from here|
/home/greg/workspace/C Part II/finalQ2/point.h|87|error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘Point<int>’|
||=== Build finished: 2 errors, 0 warnings ===|
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
T& Point<T>::operator +(T delta)
Point<T> Point<T>::operator +(T delta)
{
    x = x + delta;
    y = y + delta;

    return Point(x + delta, y + delta);
}

Some great advice here:-
http://stackoverflow.com/questions/4421706/operator-overloading
Last edited on
Thanks for the help.
Topic archived. No new replies allowed.