Error function template has already been defined

im creating a program that tests the functionality of template classes for my c++ class but i keep getting this error at the overloaded << and >> functions when i try to test the class using a double
can someone help me out
MyData.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
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
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
using namespace std;
#include <string>
template <class T> class MyData
{
  T value;
public:
	//Constructors
	MyData(T t)
	{
		value = t;
	}
	MyData()
	{
		value = 0;
	}

	//SetData
	void setData(T t)
	{
		value = t
	}
	//GetData
	T getData()
	{
		return value;
	}
	//Overload +
	MyData operator+(MyData op2)
	{
		return value+op2.value;
	}
	//Overload -
	MyData operator-(MyData op2)
	{
		return value-op2.value;
	}
	//Overload =
	MyData operator=(MyData op2)
	{
		return value = op2.value;
	}
	//Overload >
	bool operator>(MyData op2)
	{
		if (value>op2.value)
	return true;
		else
			return false;
	}
	//Overload ++
	MyData operator++(int)
	{
	return	value++;
	
	}
	
	//Overload cin
template<class T>	friend istream &operator>>(istream &stream, MyData<T> &obj)
	{
		cout << "Enter data: ";
		stream >> obj.value;
		return stream;
	}
	
//Overload cout
template<class T>	friend ostream &operator<<(ostream &stream, MyData<T> &obj)
	{
		stream << obj.value;
		return stream;
	}


};

//Create string specialization
template <> class MyData<string>
{
	string value;

public:
	MyData(string t)
	{
			value = t;
	}

	MyData()
	{
		value = "";
	}

friend istream &operator>>(istream &stream, MyData<string> &obj)
	{
		cout << "Enter data: ";
		stream >> obj.value;
		return stream;
	}
	

friend ostream &operator<<(ostream &stream, MyData<string> &obj)
	{
		stream << obj.value;
		return stream;
	}

};



Main

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
#include<iostream>
using namespace std;
#include "MyData.h"

int main()
{
	int intValue = 12;
	MyData<int> intData(intValue);
	intData++;
	cout <<"integer incremented: " << intData<<endl;
	MyData<int> secondInt(3);
	cout<<"Sum of two integers: "<< intData+secondInt<<endl;
	cout<<"Difference of two integers: "<< intData-secondInt<<endl;
	MyData<int> EqualsData;
	cout<<"Test default constructor: "<< EqualsData<<endl;
	EqualsData = secondInt;
	cout<<"Test == function: "<< EqualsData<<endl;
	if (intData >  secondInt)
		cout<<intData<<" is larger than "<<secondInt<<endl;
	MyData<double> doubleData1(12.2);
	MyData<double> doubleData2(4.2);
	cout<<doubleData1<<"+"<<doubleData2<<"="<<doubleData1+doubleData2;

MyData<string> stringData("Good");
cout<<"Test string data: "<<stringData<<endl;
	
}

You already have templace<class T> defined for your MyData class. Try using another name for your templace other than "T"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	//Overload cin
template<class X>	friend istream &operator>>(istream &stream, MyData<X> &obj)
	{
		cout << "Enter data: ";
		stream >> obj.value;
		return stream;
	}
	
//Overload cout
template<class X>	friend ostream &operator<<(ostream &stream, MyData<X> &obj)
	{
		stream << obj.value;
		return stream;
	}
Tried that out and still got the same error
You should take the definitions of the istream and ostream overloads out of the class definitions:

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
template <class T> class MyData
{
    ...

    //Overload cin
    template<class T> friend istream &operator>>(istream &stream, MyData<T> &obj);
	
    //Overload cout
    template<class T> friend ostream &operator<<(ostream &stream, MyData<T> &obj);
};

//Overload cin
template<class T>	istream &operator>>(istream &stream, MyData<T> &obj)
{
    cout << "Enter data: ";
    stream >> obj.value;
    return stream;
}

//Overload cout
template<class T>	ostream &operator<<(ostream &stream, MyData<T> &obj)
{
    stream << obj.value;
    return stream;
}

//Create string specialization
template <> class MyData<string>
{
    ...

    template <> friend istream &operator>>(istream &stream, MyData<string> &obj);
    template <> friend ostream &operator<<(ostream &stream, MyData<string> &obj);
};

template <> istream &operator>>(istream &stream, MyData<string> &obj)
{
    cout << "Enter data: ";
    stream >> obj.value;
    return stream;
}


template <> ostream &operator<<(ostream &stream, MyData<string> &obj)
{
    stream << obj.value;
    return stream;
}
Topic archived. No new replies allowed.