Templates and vectors of pairs

Oct 3, 2009 at 4:11am
I've just begun with templates, and I'm having issues with this:
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
#ifndef VECTOR2_HPP
#define VECTOR2_HPP
#include <cmath>
#include <utility>
#include <vector>

#ifndef M_PI
#define M_PI 3.1415926
#endif

#define DEGTORAD(Deg) ((Deg * M_PI) / 180.0)
#define RADTODEG(Rad) ((180.0 * Rad) / M_PI)

/*
 * Euclidian vector with many useful member functions
 */
template <class T>
class Vector2
{
	public:
		Vector2()
		{
			magnitude = 0;
			direction = 0;
		}
		Vector2(T start_magnitude, T start_direction)
		{
			magnitude = start_magnitude;
			direction = start_direction;
		}
		~Vector2()
		{
		}

		T magnitude;
		T direction;

		/*
		 * Returns an std::pair<T, T> of the coordinates of the
		 * point_numth point on the vector
		 */
		std::pair<T, T> get_point(int point_num)
		{
			std::pair<T, T> point;
			point.first = std::sin(DEGTORAD(direction)) * magnitude;
			point.second =
				std::cos(DEGTORAD(direction)) * magnitude;
			return point;
		}

		/*
		 * Returns an std::vector<std::pair<T, T>>. Each pair contains
		 * the coordinates of a point on the vector, up to num_points
		 * points (including (0, 0))
		 */
		std::vector<std::pair<T, T>> get_points(int num_points)
		{
			std::vector<std::pair<T, T>> points;
			for (int i = 0; i < num_points; i++)
			{
				std::pair<T, T> point = get_point(i);
				points.push_back(point);
			}
			return points;
		}
};

#endif // VECTOR2_HPP 


My errors:

1
2
3
4
5
6
7
8
9
Vector2.hpp:56: error: a function call cannot appear in a constant-expression
Vector2.hpp:56: error: ‘>>’ should be ‘> >’ within a nested template argument list
Vector2.hpp: In member function ‘std::vector<std::pair<_ForwardIterator, _ForwardIterator>, std::allocator<std::pair<_ForwardIterator, _ForwardIterator> > > Vector2<T>::get_points(int)’:
Vector2.hpp:58: error: ‘coords’ was not declared in this scope
Vector2.hpp:58: error: ‘>>’ should be ‘> >’ within a nested template argument list
Vector2.hpp:58: error: redeclaration of ‘std::vector<std::pair<_ForwardIterator, _ForwardIterator>, std::allocator<std::pair<_ForwardIterator, _ForwardIterator> > > coords’
Vector2.hpp:58: error: ‘<typeprefixerror>coords’ previously declared here
main.cpp: In function ‘int main()’:
main.cpp:8: error: ‘>>’ should be ‘> >’ within a nested template argument list


I really don't understand what's prompting the errors. If anybody could give me some help, I'd really appreciate it. (I'm compiling with gcc)

Thanks,
Jordy
Oct 3, 2009 at 4:26am
I don't think the errors could be any more explicit.
std::vector<std::pair<T, T>> is invalid. It's
std::vector<std::pair<T, T> >

>> is an operator, remember?
Oct 3, 2009 at 12:37pm
This problem will be addressed by C++0x
http://en.wikipedia.org/wiki/C%2B%2B0x#Angle_bracket
Oct 3, 2009 at 3:22pm
I don't think the errors could be any more explicit.
std::vector<std::pair<T, T>> is invalid. It's
std::vector<std::pair<T, T> >

>> is an operator, remember?


Oh, wow, that was amazingly simple.

I have a habit of only looking at the first error message, because the subsequent ones tend to be caused by the first message, and go away after the first one is fixed. So, I didn't but 2 and 2 together and see the the function call it was talking about must have been the >> operator.

Thank you.
Topic archived. No new replies allowed.