Vec2 Template Help

I'm working on an assignment and I'm a bit confused on what I'm supposed to do. I made a vec2d class previously for a different assignment. Now I have to make that class into a template class. The documentation says the goal is to build a 2-d vector class to make different numeric types (int, float, double).

I need to make three typedefs vec2i, vec2f, vec2d. Each of which will be an alias for vec2< int >, vec2< float >, vec2< double >. I'm just kind of confused on what I need to do to start this.

Here is my vec2 class code from the previous assignemnt and a link to my official documentation.

OFFICIAL DOCUMENTATION: http://i.imgur.com/kWZiWJn.jpg
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
89
90
91
92
93
94
95
96
#ifndef VEC2D_H
#define VEC2D_H

#include <iostream>

class vec2d
{
public:
    vec2d(double x0 = 0.0, double y0 = 0.0)
        : _x(x0), _y(y0)
    {}
    vec2d(const vec2d &v)
        : _x(v._x), _y(v._y)
    {}
    /*
    bool operator==(const vec2d &v) const
    {
        return (v[0] == v[1]);
    }
    */
    bool operator==(const vec2d &v) const
    {
        return (_x == v._x && _y == v._y);
    }
    bool operator!=(const vec2d &v)
    {
        return !(*this == v);
    }
    double operator[](int i) const
    {
        return(i == 0 ? _x : _y);
    }
    double & operator[](int i)
    {
        return(i == 0 ? _x : _y);
    }
    double get_x()
    {
        return _x;
    }
    double get_y()
    {
        return _y;
    }
    vec2d operator+() const  //positive of
    {
        return *this;
    }
    vec2d operator+(const vec2d &v) const
    {
        return vec2d(_x + v._x, _y + v._y);
    }
    vec2d & operator+=(const vec2d &v)
    {
        _x += v._x;
        _y += v._y;
        return *this;
    }
    vec2d operator-(const vec2d &v) const
    {
        return *this + -v;
    }
    vec2d & operator-=(const vec2d &v)
    {
        _x -= v._x;
        _y -= v._y;
        return *this;
    }
    vec2d operator-() const  //negative of
    {
        return vec2d(-_x, -_y);
    }
    vec2d operator*(const double c) const
    {
        return vec2d(_x * c, _y * c);
    }
     vec2d & operator*=(double c)
    {
        _x *= c;
        _y *= c;
        return *this;
    }
    vec2d operator/(const double c) const
    {
        return vec2d(_x / c, _y / c);
    }
    vec2d & operator/=(const double c)
    {
        *this = *this / c;
        return *this;
    }
    static vec2d default;
    
private:
    double _x, _y;
};
It should be very simple to make your class a template class. Read up on them here http://www.cplusplus.com/doc/tutorial/templates/ (scroll down to template classes)
Topic archived. No new replies allowed.