Overloaded operator problem

I made this class as a project so I'd better understand classes:

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
template<typename _type>
class Value{
private:
	_type _value;

public:
	Value(_type _data) :_value(_data){};
	Value(void) : _value(_type(0)){};
	~Value(void){};

	void operator=(const Value<_type>& _Value){
		_value = _Value._value;
	}

	void operator=(const _type& _data){
		_value = _data;
	}

	Value<_type>& operator+=(const Value<_type>& _Value){
		_value += _Value._value;
	return *this;
	}

	Value<_type>& operator+=(const _type& _data){
		_value += _data;
	return *this;
	}

	Value<_type>& operator++(void){
		_value++;
	return *this;
	}

	Value<_type> operator++(int){
		return Value<_type>(_value++);
	}

	friend std::ostream& operator<<(std::ostream& os, Value<_type>& _Value){ return os << _Value._value;}
	friend std::istream& operator>>(std::istream& is, Value<_type>& _Value){ return is >> _Value._value;}
};

template<typename _type>
Value<_type> operator+(const Value<_type>& _lValue, const Value<_type>& _rValue){
	Value<_type> _ret = _lValue;
	_ret += _rValue;
return _ret;
}

template<typename _type>
Value<_type> operator+(const Value<_type>& _Value, const _type& _data){
	Value<_type> _ret = _Value;
	_ret._value += _data;
return _ret;
}

template<typename _type>
Value<_type> operator+(const _type& _ldata, const _type& _rdata){
	return Value<_type>(_ldata + _rdata);
}

template<typename _type>
Value<_type> operator+(const _type& _data, const Value<_type>& _Value){
	Value<_type> _ret = _Value;
	_ret._value += _data;
return _ret;
}


The problem is if I try to do this:

1
2
3
4
5
6
#include <iostream>

int main(){
    Value<int> x(50), y(40);
    std::cout << x + y;
}


GCC will output this error:


In function `int main()':
..\main.cpp:15: error: no match for 'operator<<' in 'std::cout << operator+(const Value<_type>&, const Value<_type>&) [with _type = int](((const Value<int>&)((const Value<int>*)(&y))))'
The body of friend functions should be outside the class so move the bodies of << and >> operators
The problem is that << has higher precedence than +. To fix:

 
std::cout << ( x + y );

<< has lower precedence than + ...
Usually << has lower precedence than +. But i thought maybe in this case it doesn't, so I had already tried std::cout << (x + y);. Unfortunately, it didn't work. And neither did moving the friend functions outside, OR removing the friend statement.
I found the problem. It was on this line:
friend std::ostream& operator<<(std::ostream& os, Value<_type>& _Value){ return os << _Value._value;}.
I changed it to:
friend std::ostream& operator<<(std::ostream& os, const Value<_type>& _Value){ return os << _Value._value;}
and it worked. Probably a safety system to assure that you pass a valid reference.
Last edited on
Oh, yes, that would do it.

operator+ returns an (unnamed) instance in your case, ie a temporary, and temporaries cannot be bound to non-const references. Hence the compile error.

Topic archived. No new replies allowed.