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...