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

Recommended Answers

All 2 Replies

Quick answer: remove the template <class T> on lines 59 and 67.

Long answer: The problem here is that you are defining, in the general class template, two function template (operators << and >>). Then, in the specialization, you define a friend function. For a moment, forget the whole business about them being friend functions and that they are defined in the class declaration. The situation basically boils down to this:

//first, you have a function template coming from your general class template:
template <class T>
std::ostream& operator >>(std::ostream& out, MyData<T>& obj) {
  //..
};

//then, you have this normal function coming from your specialized class template:
std::ostream& operator >>(std::ostream& out, MyData<std::string>& obj) {
  //..
};

The problem with the above is basically that your compiler decides that the second function looks too much like a specialization or an implementation of the first function template, and thus, tells you "Error function template has already been defined". This is somewhat compiler-specific, GCC accepts your code (at least my version of it), I would suspect some compilers don't (working out correctly the rules related to function templates and function overloads, especially if friendship is involved, has been notoriously difficult for some compilers).

In any case, it doesn't really matter since your code is somewhat wrong whether the compiler accepts it or not. The thing is, your general template doesn't need to declare a templated friend function, you only need an ordinary friend function. And thus, if you follow my "Quick answer" it will solve your problem, I'm sure.

thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.