Problems with function overload

Hello, I am trying to create a program that uses function overload that prompts the user to input the height and width to calculate area three times. This program requires the use of the void function. For some reason i get the "expected Unqualified id before '{' token " error. I am a beginner in c++ and any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
#include <cmath>

using namespace std;

void output (double, double );
void output2 (double, double);
void output3 (double, double);
int main()
{
   double x1,y1;
   double num1,num2;
   double height,width,area;
   cout << "Please enter height Rectangle: ";
   cin >> num1;
   cout << "please enter the width of the Rectangle: ";
    cin >> num2;
   area = (height * width);
   return 0;

}
   void output()
   {
       double height,width,area;
    area = height * width;
       cout << "This is the height of Rectangle one" << height << endl;
       cout << "this is the width of Rectangle one" << width << endl;
       cout << "This is the area of Rectangle one" << area << endl;
   }
{
   double num1,num2;
   double height,width,area;
   cout << "Please enter height Rectangle: ";
   cin >> num1;
   cout << "please enter the width of the Rectangle: ";
    cin >> num2;
   area = (height * width);
   return 0;
}
void output()
   {
       double height,width,area;
       area = height * width;
       cout << "This is the height of Rectangle two" << height << endl;
       cout << "this is the width of Rectangle two" << width << endl;
       cout << "This is the area of Rectangle two" << area << endl;
   }
{
   double num1,num2;
   double height,width,area;
   cout << "Please enter height Rectangle: ";
   cin >> num1;
   cout << "please enter the width of the Rectangle: ";
    cin >> num2;
   area = (height * width);
   return 0;

}
 void output()
   {
       double height,width,area;
       area = height * width;
       cout << "This is the height of Rectangle three" << height << endl;
       cout << "this is the width of Rectangle three" << width << endl;
       cout << "This is the area of Rectangle three" << area << endl;
   }


It's unlikely that your assignment is to copy a function 3 times with minor changes. What is the point? You are probably supposed to call the same function 3 times.
There are a lot of problems here.
1. lines 30-39 and lines 48-58 are incorrect and will cause compile errors you are seeing -- you're anonymously enclosing global stuff in curly brackets and this is not allowed. Put them under a function or so
2. lines 6-8 show three function prototypes without even a comment as to what makes them different or what they're supposed to do. Then in implementation, you make different output() functions, which have nothing to do with the prototypes because the parameters are different
3. then you have the same function implementation, with signature void output() , three times. This is also not allowed and will result in "redefinition" error.

You should probably delete 2/3 of everything you have there and start with a simple working function. Then read carefully about what it means to overload a function.

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

using namespace std;

void Play()
{
    cout << "Tom plays the piano\n";
}
void Play(string note)
{
    cout << "Tom plays the note "<<note<<" on the Piano\n";
}
void Play(string note, string instrument)
{
    cout << "Tom plays the note "<<note<<" on the "<<instrument<<endl;
}

int main()
{
    Play();
    Play("La");
    Play("Mi", "Flute");

    return 0;
}
Last edited on
Topic archived. No new replies allowed.