-
June 25th, 2002, 04:01 PM
#1
problems with overloading the operator +
Hi,I am trying to add two objects of a class into a third one of the same class
for instance if class name kota and object name would be
kota A,B;
C=A+B;
but i am writting this to the kota.h
kota operator+(kota SA_1,kota SA_2);
and in the kota.cpp
kota kota:perator+(kota SA_1,kota SA_2)
{ kota C;
return C;
}
don't mind the rest of the algorithm the problem is that the compiler shows that
c:\windows\desktop\13331.rar\kota.h(23) : error C2804: binary 'operator +' has too many parameters
Error executing cll.exe.
Creating browse info file...
application.exe - 1 error(s), 0 warning(s)
please help otherwise how can i write so i can add the A+B(some values of them to C)
thanks.
-
June 25th, 2002, 04:18 PM
#2
Either write your operator+ as a member function of kota with 1 argument, or write it as a global function with 2 arguments.
Member:
kota kota::operator+( kota SA_2 ) const
{
kota C = ???; // add this and SA_2
return C;
}
Global:
kota operator+( kota SA_1, kota SA_2 )
{
kota C = ???; // add SA_ 1 and SA_2
return C;
}
Best regards,
John
-
June 25th, 2002, 04:52 PM
#3
it doesn't make any sense
i've tried both of those and it says the same thing again
Anyway this also what the C=A+B should do when it woulc be called through the main function
C.set(SA_2.exp_date.get_day(),SA_2.exp_date.get_month(),SA_2.exp_date.get_year());
//set(int,int,int);
i don't care about the algorithimic body i care about the compiler erros.
THanks for the help
-
June 25th, 2002, 05:30 PM
#4
Overloading Binary operators:
prototype:
kota operator+(kota SA_1);
definition:
kota kotaerator+(kota SA_1)
{
kota C;
_________
_________
return C;
}
Overloading Binary operator using friend:
prototype:
friend kota operator+(kota SA_1,kota SA_2)
definition:
kota kota:perator+(kota SA_1,kota SA_2)
{
kota C;
=========
=========
return C;
}
This is supposed to work.
best of luck.
-
June 26th, 2002, 02:07 AM
#5
I tested the following, they works:
1.----------------------------------
#ifndef _PT_CLASS_
#define _PT_CLASS_
class plustest {
//friend const plustest operator+(plustest& lhs, plustest& rhs);
public:
plustest(int nn)
{
n = nn;
};
plustest()
{
n = 10;
};
~plustest()
{
n = 0;
};
int n;
};
const plustest operator+(plustest& lhs, plustest& rhs);
#endif
#include "plustest.h"
const plustest operator+(plustest& lhs, plustest& rhs)
{
return plustest(lhs.n + rhs.n);
};
for this solution, i can also write the operator+ like the following:
const plustest operator+(plustest& lhs, plustest& rhs)
{
plustest pt;
pt.n = lhs.n + rhs.n;
return pt;
};
the const plustest operator+(plustest& lhs, plustest& rhs) can also be declared in class as friend as what i comment out.
2.------------------------------------------------------------
#ifndef _PT_CLASS_
#define _PT_CLASS_
class plustest {
public:
plustest(int nn)
{
n = nn;
};
plustest()
{
n = 10;
};
~plustest()
{
n = 0;
};
const plustest& operator+(plustest& rhs)
{
this->n = this->n + rhs.n;
return *this;
}
int n;
};
#endif
-
June 26th, 2002, 02:57 AM
#6
BTW - pass the arguments to the operator as const references rather than value args - it should be a bit more efficient:
Code:
kota operator+(const kota& lhs, const kota& rhs);
Also there's no point in a const value return from the function. i.e.
Code:
const kota& operator+(const kota&, const kota&)
makes sense, but
Code:
const kota operator+(const kota&, const kota&)
doesn't.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|