problem with Overloading

Hello everyone,

I need to overload these two functions, the problem is, I am lots rusty in overloading. Please indicate me to my error and if possible, point me to the solution.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
using namespace std;

double MySum(double X1, double X2) { return X1+X2; }
double MySum(double Y1, double Y2) { return Y1+Y2; }

int main ()
{

  X1 = 3;
  X2 = 5;
  Y1 = 1;
  Y2 = 2;

  return 0;
}
Your functions are currently identical in terms of number of parameters, parameter types and return type.

http://www.cplusplus.com/doc/tutorial/functions2/
In C++, two different functions can have the same name if their parameters are different; either because they have a different number of parameters, or because any of their parameters are of a different type.

...

Note that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.
Thanks,

I forgot to declare the variables inside the int main()... Thank you!
Would this code be correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>

using namespace std;

double MySum(double X1,    int X2) { return X1+X2; } // WILL ADD THE X COORDINATES
float  MySum(int Y1,     float Y2) { return Y1+Y2; } // WILL ADD THE Y COORDINATES

int main ()
{
  double X1 = 4.9;   // ASSIGN THE VARIABLE X1 TO A DECIMAL VALUE
   int    X2 =   5;   // ASSIGN THE VARIABLE X2 TO A DECIMAL VALUE
   int    Y1 =   1;   // ASSIGN THE VARIABLE Y1 TO A WHOLE-NUMBER VALUE
   float  Y2 = 2.7;   // ASSIGN THE VARIABLE Y2 TO A WHOLE-NUMBER VALUE

   cout << "X1 : " << setw(3) <<  X1 << "   " << "Y1 : " << setw(3) << Y1 << endl;
   cout << "X2 : " << setw(3) <<  X2 << "   " << "Y2 : " << setw(3) << Y2 << endl << endl;


   cout << char(996) << "X : " << setw(3) << MySum(X1, X2) << "   " << char(996) << "Y : " << setw(3) << MySum(Y1, Y2) << endl;  // THE METHODS ARE
                                                                                                                                 // NOW PERFORM THE
   return 0;                                                                                                                     // CALCULATIONS
}
Topic archived. No new replies allowed.