Function Overloading

Working on another section and encountered a problem. Maybe its me having stayed up all night doing calculus, but I just seem to continue running into errors.

Here is the code. It is meant to have the user input a value, then display it back to them using a function. Then it would like for me to repeat the task using function overloading.

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
  // C5HW question 3

#include <iostream>
#include <string>

using namespace std;

int askUser(int high, int low = 1);
int askUser2();

int main()
{
	int value;
    int number = askUser(15);
    cout << "You entered: " << number << "\n\n";
    
    number = askUser2();
    cout << "You entered: " << number << "\n\n";
    
    return 0;
}

int askUser(int high, int low)
{
    int input;
    do
    {
        cout << "Please enter a number" << " (" << low << " - " << high << "): ";
        cin >> input;
    } while (input > high || input < low);

    return input;
}

int AskUser2()
{
	int value;
	cin >> value;
	return(value);
}
Last edited on
closed account (E0p9LyTq)
A couple of very simple changes and you have successfully overloaded two functions.

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
#include <iostream>
#include <string>

using namespace std;

int askUser(int high, int low = 1);
int askUser();

int main()
{
   int value = 0;
   int number = askUser(15);
   cout << "You entered: " << number << "\n\n";

   number = askUser();
   cout << "You entered: " << number << "\n\n";

   return 0;
}

int askUser(int high, int low)
{
   int input;
   do
   {
      cout << "Please enter a number" << " (" << low << " - " << high << "): ";
      cin >> input;
   }
   while (input > high || input < low);

   return input;
}

int askUser()
{
   int value;
   cin >> value;
   return(value);
}
Still getting an error. "Id returned 1 exit status"
ld is your linker. The code works fine - https://ideone.com/4aie7x . Your computer compiler/linker setup is bad, or you're not compiling the code shown above.
Last edited on
closed account (E0p9LyTq)
That "error" is not the actual error. It means the compiler couldn't successfully compile your source.

It is your compiler telling you, "hey! I found an error!"

There should be other output lines above that one. Post those. One or more will be the actual error(s).

For example if the askUser() function definition was actually named AskUser():

C:\Programming\My Projects\main.cpp In function 'int main()':
11 8 C:\Programming\My Projects\main.cpp [Warning] unused variable 'value' [-Wunused-variable]
C:\Programming\My Projects\main.o main.cpp:(.text+0x6b): undefined reference to `askUser()'
C:\Programming\My Projects\collect2.exe [Error] ld returned 1 exit status
25 C:\Programming\My Projects\Makefile.win recipe for target 'Project1.exe' failed

Last edited on
C5_HW2.cpp:(.text+0x56): undefined reference to `askUser()'

Is this what you mean? It's showing this message on a few other programs im working on, but some of them do work so I'm not sure if its the compiler.
Oh, I found out what happened. Appreciate all of the help! You guys are the best.
closed account (E0p9LyTq)
Oh, I found out what happened

What was the problem?

Help others who might have the same/similar problems by telling us what was wrong and how you fixed it.
Overlooked one of the functions and forgot to delete a 2. I feel so dumb because I forgot that overloading the function means having the same name but different parameters. Thank you once again! Will mark as solved.
closed account (E0p9LyTq)
Making mistakes, even simple ones such as yours, is how we learn and improve programming skills.

The number of dumb things I still do when mashing up some code, OY! :)

Next time it will be easier to spot and solve when you have a function name mismatch. :D
Topic archived. No new replies allowed.