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
#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
#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;
}