4 Replies - 1003 Views - Last Post: 30 December 2010 - 06:20 AM Rate Topic: -----

#1 cutecutekid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-November 10

Operator Overloading

Posted 30 December 2010 - 05:17 AM

Hi, I am having some problem with operator overloading.
#include <iostream>
using namespace std;

class fdata 
{
	float value;
public:
	fdata	( float v )	{ value = v; }
};

class data 
{
	int value;
public:
	data	( int v )	{ value = v; }
	friend data operator+ ( data &, data & );
	friend data operator+ ( int, data & );
	friend data operator+ ( data &, int );
	friend data operator- ( data &, data & );
	friend data operator* ( data &, data & );
	friend data operator/ ( data &, data & );
	friend data operator+ ( data &, fdata & );
	friend data operator+ ( fdata &, data & );
	friend ostream& operator<<	(ostream& out, data &d);
	friend istream& operator>>	(istream& in, data &d);
	data operator++ ( void );
	data operator++ ( int ignore );
};

ostream& operator<<	(ostream& out, data &d)
{
	return out << d.value << endl;
}

istream& operator>> (istream& in, data &d)
{
	in >> d.value ;
	return in;
}
data operator+ ( data &d1, data &d2 )
{
	d1.value = d1.value + d2.value;
	return d1;
}
data operator+ ( int a, data &d )
{
	d.value = a + d.value;
	return d;
}
data operator+ ( data &d, int a )
{
	d.value = d.value + a;
	return d;
}
data operator- ( data &d1, data &d2 )
{
	d1.value = d1.value - d2.value;
	return d1;
}
data operator* ( data &d1, data &d2 )
{
	d1.value = d1.value * d2.value;
	return d1;
}
data operator/ ( data &d1, data &d2 )
{
	d1.value = d1.value / d2.value;
	return d1;
}

data data::operator++ ( void )
{
	data d(1);
	(*this) = (*this) + d;
	return (*this);
}
data data::operator++ ( int ignore ) 
{
	data returndata = (*this);
	data d(1);
	(*this) = (*this) + d;
	return returndata;
}
data operator+ ( data &d1, fdata &fd1 )
{
	data d2(0);
	d2 = d1 + fd1;
	return d2;
}
data operator+ ( fdata &fd1, data &d1)
{
	fdata fd2(0.0);
	fd2 = fd1 + d1;
	return fd2;
}
int main (void)
{
	data d(1);
	data d1(2);
	data d2(3);
	fdata fd1(1);
	operator+(d1, d2);
	operator<<(cout, d);
	operator-(d, d1);
	operator<<(cout, d);
	operator*(d1, d);
	operator<<(cout, d);
	operator/(d, d1);
	operator<<(cout, d);
	operator+(d, 45);
	operator<<(cout, d);
	operator+(3, d);
	operator<<(cout, d);
	++d;
	operator<<(cout, d);
	d++;
	operator<<(cout, d);
	operator+(d, fd1);
	operator<<(cout, d);
	operator+(fd1, d);
	operator<<(cout, fd1);

	return 0;
}



The function that i am having problem with is
friend data operator+ ( data &, fdata & );
friend data operator+ ( fdata &, data & );


When i compile, the 1st function doesn't give any problem, but the 2nd function gives some errors.
What i am trying to achieve here is making this two classes have the expression:
data1 = data2 + fdata1
fdata1 = fdata2 + data1
So what has i done wrong with the 2nd function and is my 1st function correct? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Operator Overloading

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Operator Overloading

Posted 30 December 2010 - 05:38 AM

Copy and paste your errors exactly as they appear please.
Was This Post Helpful? 0
  • +
  • -

#3 cutecutekid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-November 10

Re: Operator Overloading

Posted 30 December 2010 - 05:44 AM

View PostJackOfAllTrades, on 30 December 2010 - 04:38 AM, said:

Copy and paste your errors exactly as they appear please.

1>c:\users\cutecutekid\documents\visual studio 2008\projects\acpp_week09\acpp_week09\exer01.cpp(93) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'data' (or there is no acceptable conversion)
1> c:\users\cutecutekid\documents\visual studio 2008\projects\acpp_week09\acpp_week09\exer01.cpp(9): could be 'fdata &fdata::operator =(const fdata &)'
1> while trying to match the argument list '(fdata, data)'
1>c:\users\cutecutekid\documents\visual studio 2008\projects\acpp_week09\acpp_week09\exer01.cpp(94) : error C2664: 'data::data(int)' : cannot convert parameter 1 from 'fdata' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\cutecutekid\documents\visual studio 2008\projects\acpp_week09\acpp_week09\exer01.cpp(121) : error C2665: 'operator <<' : none of the 9 overloads could convert all the argument types
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(700): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(738): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(785): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(909): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(916): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(923): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(930): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\users\cutecutekid\documents\visual studio 2008\projects\acpp_week09\acpp_week09\exer01.cpp(24): or 'std::ostream &operator <<(std::ostream &,data &)'
1> while trying to match the argument list '(std::ostream, fdata)'
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Operator Overloading

Posted 30 December 2010 - 05:52 AM

Look at this method:
data operator+ ( fdata &fd1, data &d1)
{
	fdata fd2(0.0);
	fd2 = fd1 + d1;
	return fd2;
}



What did you say it would return, and what are you ACTUALLY returning?
Was This Post Helpful? 0
  • +
  • -

#5 cutecutekid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-November 10

Re: Operator Overloading

Posted 30 December 2010 - 06:20 AM

View PostJackOfAllTrades, on 30 December 2010 - 04:52 AM, said:

Look at this method:
data operator+ ( fdata &fd1, data &d1)
{
	fdata fd2(0.0);
	fd2 = fd1 + d1;
	return fd2;
}



What did you say it would return, and what are you ACTUALLY returning?

ok so the return type is wrong. it should b fdata instead of data. If i don't return anything, they will give me
error C4716: 'operator+' : must return a value
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1