Operator overloading problems

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm having problems trying to compile the following sample code:

////////////////////////////////////////////////////
// Start of sample code
// The lines failing to compile are indicated by comments
/////////////////////////////////////////////////

#include <iostream>

class Byte
{
private:
	char value;
public:
	Byte ( ) {
		value = 0;
	}
	Byte ( char v ) {
		value = v;
	}

	int Get ( ) {
		return (int) value;
	}

	//
	// Some operator overloadings
	//

	// Byte = int
	void operator= (int b) {
		value = b;
	}

	// Byte++ (fetch and then increment)
	const Byte operator++ ( ) {
		Byte b (value);
		value++;
		return b;
	}

	// ++Byte (increment and then fetch)
	const Byte& operator++ (int) {
		value++;
		return *this;
	}

	// Byte-- (fetch and then increment)
	const Byte operator-- ( ) {
		Byte b (value);
		value--;
		return b;
	}

	// --Byte (increment and then fetch)
	const Byte& operator-- (int) {
		value--;
		return *this;
	}

	// Byte + Byte
	Byte operator+ (Byte& b) const {
		return Byte (this->value + b.value);
	}

	// Byte + int
	Byte operator+ (int& i) const {
		return Byte (value + i);
	}

	// Byte - Byte
	Byte operator- (Byte& b) const {
		return Byte (value - b.value);
	}

	// Byte - int
	Byte operator- (int& i) const {
		return Byte (value - i);
	}

	// Byte * Byte
	Byte operator* (Byte& b) const {
		return Byte (value * b.value);
	}

	// Byte * int
	Byte operator* (int& i) const {
		return Byte (value * i);
	}

	// Byte / Byte
	Byte operator/ (Byte& b) const {
		return Byte (value / b.value);
	}

	// Byte / int
	Byte operator/ (int& i) const {
		return Byte (value / i);
	}

	// Byte == Byte ?
	bool operator== (Byte& b) const {
		return (value == b.value ? true : false);
	}

	// Byte > Byte ?
	bool operator> (Byte& b) const {
		return (value > b.value ? true : false);
	}

	// Byte >= Byte ?
	bool operator>= (Byte& b) const {
		return (value >= b.value ? true : false);
	}

	// Byte < Byte ?
	bool operator< (Byte& b) const {
		return (value < b.value ? true : false);
	}

	// Byte <= Byte ?
	bool operator<= (Byte& b) const {
		return (value <= b.value ? true : false);
	}
};


int main ( ) {

	Byte b;
	Byte b1 (2);
	Byte b2 (3);
	Byte b3 (5);


	b = b1 + b2 + b3;	// OK
	b = b1 + b2 - b3;	// OK
	b = b1 * b2 / b3;	// OK
	b = b1 * b2 + b3;	// OK
	b = b1 / b2 * b3;	// OK
	b = b1 + b2 * b3;	// FAIL
	b = b1 - b2 / b3;	// FAIL

	return 0;
}
////////////////////// end of sample code ///////////////////////

As you can see, the problem occurs when I try to use the overloaded '*' or
'/' after the '+' or '-' in a single expression.
I've tryed to compile with gcc 3.2.2, 3.2.3, 3.3.1 and even 2.95, but with
no success.
The error message is (gcc 3.2.2):
bruno@vega:~/devel/C/objcc$ g++ -Wall -o Samp sample.cc
sample.cc: In function `int main()':
sample.cc:134: no match for `Byte& + Byte' operator
sample.cc:55: candidates are: Byte Byte::operator+(Byte&) const
sample.cc:60:                 Byte Byte::operator+(int&) const
sample.cc:135: no match for `Byte& - Byte' operator
sample.cc:65: candidates are: Byte Byte::operator-(Byte&) const
sample.cc:70:                 Byte Byte::operator-(int&) const

Thank You.

Bruno.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux