Defining functions of template class

I have been having trouble defining functions of a template class ;this is my first time with template classes . In the following code i have been getting 41 errors in vs 2012 on windows 7 .

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
  #include<string.h>
#include "stdafx.h"

using namespace std;

#pragma once

template <class T>
class Point {
public :
	Point();
	Point(T ,T);
	Point(const T&);
	~Point();

	Point<T>& operator=(const Point<T>&);
	T x() const;
	T y() const;

	string toString() const;

protected:
	T _x,_y;
};


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 T& p) : _x(p._x) _y(p._y) { }

template <class T>
Point<T>& Point<T>::operator=(const Point<T>& p) : _x(p._x) _y(p._y) {

	return *this
}

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

template <class T>
T point<T>::x() const {
	return _x; 
}

template <class T>
T point<T>::y() const { 
	return _y;
}

template <class T>
string Point<T>::toString() const {
	ostringstream output;
	output << "(" << _x <<","<<_y<<")";
	return output.str();
}



The error starts with line number 46 : missing ; before < ?

Thanks in advance!
> i have been getting 41 errors
it would be nice to read at least one.


these are the messages from clang++
28, 34, 37
error: missing ',' between base or member initializers
_x(x), _y(y)

31, 43
error: out-of-line declaration of a member must be a definition
you have not provided a body

37
error: only constructors take base initializers
operator= is not a constructor

46, 56
error: no template named 'point'; did you mean 'Point'?
yes, you did

39
error: expected ';' after return statement
return *this;



Apart from that, to use std::string you need to #include <string> (not string.h)
you also need to #include <sstream> to use std::ostringstream

Also, line 4 would pollute anything that includes your header.
thanks !

can you also tell me what do you mean by your last line , I was not able to grasp the idea behind it : "line 4 would pollute anything that includes your header"
Why I think that you've never tried to debug it...
Some of them really easy spotted..

1
2
3
4
#include <string>
#include <sstream>

using namespace std;


1
2
template <class T>
Point<T>::Point(T x, T y) : _x(x) _y(y) { }
1
2
template <class T>
Point<T>::Point(T x, T y) : _x(x),_y(y) { }


1
2
template <class T>
Point<T>::Point();
1
2
template <class T>
Point<T>::Point(){}


1
2
template <class T>
Point<T>::Point( const T& p) : _x(p._x) _y(p._y) { }
1
2
template <class T>
Point<T>::Point( const T& p) : _x(p._x),_y(p._y) { }


1
2
3
4
5
template <class T>
Point<T>& Point<T>::operator=(const Point<T>& p) : _x(p._x) _y(p._y) {

	return *this
}
1
2
3
4
5
6
template <class T>
Point<T>& Point<T>::operator=(const Point<T>& p){
	_x = p._x;
	_y = p._y;
	return *this;
}


1
2
template <class T>
Point<T>::~Point();
1
2
template <class T>
Point<T>::~Point(){}


1
2
3
4
template <class T>
T point<T>::x() const {
	return _x; 
}
1
2
3
4
template <class T>
T Point<T>::x() const {
	return _x; 
}


1
2
3
4
template <class T>
T point<T>::y() const { 
	return _y;
}
1
2
3
4
template <class T>
T Point<T>::y() const { 
	return _y;
}
Last edited on
> line 4 would pollute anything that includes your header
An example http://www.cplusplus.com/forum/beginner/31829/#msg172875
Topic archived. No new replies allowed.