Value.h

#ifndef VALUE_H
#define VALUE_H

#include <iostream>

template<typename DataType>
class Value
{
public:
	Value(DataType val);
	Value(const Value<DataType>& aValue);

	Value& operator =(const Value& aValue);
	Value& operator +(const Value& aValue);

	void SetValue(DataType val) { _value = val; }
	DataType GetValue() const { return _value; }

	template<typename DataType>
	friend std::ostream& operator <<(std::ostream& output, const Value<DataType>& aValue);

	template<typename DataType>
	friend std::istream& operator >>(std::istream& input, Value<DataType>& aValue);

protected:
	DataType _value;
};
#endif


template<typename DataType>
Value<DataType>::Value(DataType val)
{
	_value = val;
}


template<typename DataType>
Value<DataType>::Value(const Value& aValue)
{
	_value = aValue._value;
}


template<typename DataType>
Value<DataType>& Value<DataType>::operator =(const Value& aValue)
{
	_value = aValue._value;

	return *this;
}


template<typename DataType>
Value<DataType>& Value<DataType>::operator +(const Value<DataType>& aValue)
{
	return (_value + aValue._value);
}


template<typename DataType>
std::ostream& operator <<(std::ostream& output, const Value<DataType>& aValue)
{
	output << aValue._value;

	return output;
}


template<typename DataType>
std::istream& operator >>(std::istream& input, Value<DataType>& aValue)
{
	input >> aValue._value;

	return input;
}

main.cpp

#include <iostream>
#include "Value.h"

int main ()
{
	Value<int> naturalNumber(0);
	Value<double> realNumber(0.0);

	naturalNumber.SetValue(10);
	realNumber.SetValue(0.5);

	std::cout << naturalNumber + realNumber << std::endl;

	std::cin.get();
	return 0;
}

I've made an overloading for the (+) operator but still getting this problem:

error C2679: binary '+' : no operator found which takes a right-hand operand of type 'Value<DataType>' (or there is no acceptable conversion)

I think I'm not using the templates properly...

template<typename DataType>
class Value
{
   // ...
   Value& operator +(const Value& aValue);
   // ..
};
template<typename DataType>
Value<DataType>& Value<DataType>::operator +(const Value<DataType>& aValue)
{ /* ... */ }

this operator+() only allows two Value template instantiations for the same DataType to be used. ie. you could add a Value<int> to another Value<int>, but not a Value<int> to a Value<short>.

also, the operator+ is not const-correct and tries to return a reference to an anonymous temporary.

perhaps this is what you intended

template<typename DataType>
class Value
{
public:
  Value(DataType val);
  // ...
  template< typename other >
  Value<DataType> operator+ ( const Value<other>& that ) const
  { return Value<DataType>( _value + that.GetValue() ); }
  // ...
  const DataType& GetValue() const { return _value; }
  //...
 protected:
  DataType _value;
};
template<typename DataType>
class Value
{
public:
  Value(DataType val);
  // ...
  template< typename other >
  Value<DataType> operator+ ( const Value<other>& that ) const
  { return Value<DataType>( _value + that.GetValue() ); }
  // ...
  const DataType& GetValue() const { return _value; }
  //...
 protected:
  DataType _value;
};

Thanks man... This helped me a lot :,)

also I Noticed that:
Using DataType instead of other worked better. For example when using other i couldn't add 0.5 to 10.

Nevermind this post... I can't delete posts so it stays..

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.