public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Operator overloading problems
@ 2003-10-17 15:50 lrtaylor
  2003-10-17 16:55 ` bruno
  0 siblings, 1 reply; 6+ messages in thread
From: lrtaylor @ 2003-10-17 15:50 UTC (permalink / raw)
  To: bruno, falk.hueffner; +Cc: gcc-help

Note that your assignment operator for int's should also normally return
a Byte reference rather than void.

Lyle

-----Original Message-----
From: bruno@codata.com.br [mailto:bruno@codata.com.br] 

Ok. It worked with const references. Thank you.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Operator overloading problems
  2003-10-17 15:50 Operator overloading problems lrtaylor
@ 2003-10-17 16:55 ` bruno
  0 siblings, 0 replies; 6+ messages in thread
From: bruno @ 2003-10-17 16:55 UTC (permalink / raw)
  To: lrtaylor; +Cc: gcc-help

Fixed:

    // Byte = int
    const Byte& operator= (int b) {
        value = b;
        return *this;
    }

Thaks.

Bruno


> Note that your assignment operator for int's should also normally
> return a Byte reference rather than void.
>
> Lyle
>
> -----Original Message-----
> From: bruno@codata.com.br [mailto:bruno@codata.com.br]
>
> Ok. It worked with const references. Thank you.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Operator overloading problems
  2003-10-17 12:27 ` Falk Hueffner
@ 2003-10-17 13:04   ` bruno
  0 siblings, 0 replies; 6+ messages in thread
From: bruno @ 2003-10-17 13:04 UTC (permalink / raw)
  To: falk.hueffner; +Cc: gcc-help

> bruno@codata.com.br writes:
>
>> Hi all,
>>
>> I'm having problems trying to compile the following sample code:
>>
>> 	// Byte / Byte
>> 	Byte operator/ (Byte& b) const {
>> 		return Byte (value / b.value);
>> 	}
>
> The argument needs to be a const reference. In this case, since Byte is
> so small, I'd just pass it by value, though.
>

Ok. It worked with const references. Thank you.


Bruno.

> --
> 	Falk



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Operator overloading problems
  2003-10-17 12:07 bruno
  2003-10-17 12:26 ` Nathan Sidwell
@ 2003-10-17 12:27 ` Falk Hueffner
  2003-10-17 13:04   ` bruno
  1 sibling, 1 reply; 6+ messages in thread
From: Falk Hueffner @ 2003-10-17 12:27 UTC (permalink / raw)
  To: bruno; +Cc: gcc-help

bruno@codata.com.br writes:

> Hi all,
> 
> I'm having problems trying to compile the following sample code:
> 
> 	// Byte / Byte
> 	Byte operator/ (Byte& b) const {
> 		return Byte (value / b.value);
> 	}

The argument needs to be a const reference. In this case, since Byte
is so small, I'd just pass it by value, though.

-- 
	Falk

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Operator overloading problems
  2003-10-17 12:07 bruno
@ 2003-10-17 12:26 ` Nathan Sidwell
  2003-10-17 12:27 ` Falk Hueffner
  1 sibling, 0 replies; 6+ messages in thread
From: Nathan Sidwell @ 2003-10-17 12:26 UTC (permalink / raw)
  To: bruno; +Cc: gcc-help

bruno@codata.com.br wrote:

> 	// Byte - int
> 	Byte operator- (int& i) const {
Byte operator- (int const &i) const {
> 	// Byte * Byte
> 	Byte operator* (Byte& b) const {
Byte operator* (Byte const &b) const {

likewise for the other operators.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
          The voices in my head said this was stupid too
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Operator overloading problems
@ 2003-10-17 12:07 bruno
  2003-10-17 12:26 ` Nathan Sidwell
  2003-10-17 12:27 ` Falk Hueffner
  0 siblings, 2 replies; 6+ messages in thread
From: bruno @ 2003-10-17 12:07 UTC (permalink / raw)
  To: gcc-help

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-10-17 16:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-17 15:50 Operator overloading problems lrtaylor
2003-10-17 16:55 ` bruno
  -- strict thread matches above, loose matches on Subject: below --
2003-10-17 12:07 bruno
2003-10-17 12:26 ` Nathan Sidwell
2003-10-17 12:27 ` Falk Hueffner
2003-10-17 13:04   ` bruno

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).