4

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;
 }
ildjarn
  • 62,044
  • 9
  • 127
  • 211
rick
  • 913
  • 6
  • 13
  • 28

7 Answers7

13

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
6

The return type is not part of the function signature.

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);.

1.3.11 signature

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Is there an explanation as to why this design choice was made? – Manux Feb 22 '12 at 16:27
  • @Manux I'm guessing that you might want different methods called from different contexts. But I don't know exactly, there are little explanations to why decisions are take in the standard. – Luchian Grigore Feb 22 '12 at 16:28
3

You can't overload the add function based only on its return type -- void or time.

Sasha Goldshtein
  • 3,499
  • 22
  • 35
3

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.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
2

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

Grammin
  • 11,808
  • 22
  • 80
  • 138
0

Rick,

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.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
0

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.

Asha
  • 11,002
  • 6
  • 44
  • 66