Reputation: 931
I am using borland turbo C++ complier (4.5). This is my code but i am getting error as follows: Multiple declaration for 'time::add(time)'.Here i am juss overloading the add() three times the error comes in the 3rd overload i.e in "void add(time t1)".
#include<iostream.h>
#include<conio.h>
class time
{
int h,m;
public:
void input()
{
cout<<"\n Enter hour :";
cin>>h;
cout<<"\n Enter min :";
cin>>m;
}
void display()
{
cout<<"\n time is : "<<h<<":"<<m;
}
void add(time t1,time t2)
{
h=t1.h+t2.h+(t1.m+t2.m)/60;
m=(t1.m+t2.m)%60;
}
time add(time t1)
{
time t3;
t3.h=h+t1.h+(m+t1.m)/60;
t3.m=(m+t3.m)%60;
return t3;
}
void add(time t1)
{
h=h+t1.h+(m+t1.m)/60;
m=(m+t1.m)%60;
}
};
int main()
{
time t1,t2,t3;
t1.input();
t2.input();
t3.add(t1,t2);
t3.display();
t3=t2.add(t1);
t3.display();
t2.add(t1);
t2.display();
return 0;
}
Upvotes: 4
Views: 551
Reputation: 33139
The problem is the
time add(time t1)
and the:
void add(time t1)
You cannot overload this way. You can have only one add
with a single time
parameter.
Upvotes: 0
Reputation: 24341
In C++, you cannot overload a function on its return type, which is what you are attempting to do. You can only overload a function on its parameters or const-ness.
I'd also consider moving to a more modern compiler, Borland C++ 4.5 is way outdated and doesn't even conform to the first C++ standard, let alone the current one.
Upvotes: 3
Reputation: 500167
In C++, you can't overload functions solely on return type:
time add(time t1) {...}
void add(time t1) {...}
I would suggest replacing the three add()
function with overloaded operators +
and +=
. This would resolve the ambiguity, and would also make it clear which of the operations modify the object, and which return a new object.
Upvotes: 13
Reputation: 258548
Function name, modifiers and parameters are. Keeping the function name and changing the others results in overloading.
This is so because the compiler can't possibly know which of the methods to call if you just write add(t);
.
the information about a function that participates in overload resolution (13.3): its parameter-type-list (8.3.5) and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared. [...]
This means that, if the function is part of the class, you can also overload it by adding a const
modifier.
Also, the parameter types are changed if declared const
.
Upvotes: 6
Reputation: 11232
Function overloading can not be done only based on return type as compiler will not come to know which function to call due to the implicit conversions that may exist between the return type and the assigned variable.
Upvotes: 0
Reputation: 12205
For function overloading to work you have to have different parameters in your overloads, you may not overload return values.
void add(time t1,time t2)
void add(time t1)
is fine,
time add(time t1)
void add(time t1)
is not
Upvotes: 2
Reputation: 3519
You can't overload the add
function based only on its return type -- void
or time
.
Upvotes: 3