# Overloading Functions

Page 1 of 1

## 6 Replies - 5034 Views - Last Post: 26 October 2006 - 10:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20123&amp;s=e92694a17d0e3e395bb90215c78b73b3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 livefromyadkin

• New D.I.C Head

Reputation: 0
• Posts: 24
• Joined: 22-September 06

# Overloading Functions

Posted 26 October 2006 - 08:40 AM

Hello once again, I'm having problems understand overloading functions and really functions in general. But this lab I have been working on is throwing me off completely and I know you guys will know the answer. I have to write a program that computes and displays the charges for a patient's hospital stay. First it ask if the patient was admitted as an in-patient or and out-patient. The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other functions accepts arguments for out-patients and both should return the total charges. Here is my code so far...

```#include <iostream>
#include <iomanip>
using namespace std;

double addcharges(int, double);
double addcharges(double);

int main()
{
int inout, days;
double charges, rate, medcharges;

cout<<"***********************************************\n";
cout<<"*   Press the either number and press enter   *\n";
cout<<"*   1. In-patient			2. Out-patient   *\n";
cout<<"***********************************************\n";
cin>>inout;
if ( inout < 1 || inout > 2)
{
cout<<"You did not input a 1 or a 2. Reenter number: \n";
cin>>inout;
}

switch(inout)
{
case 1:
{
cout<<"Enter the number of days spent in the hospital. \n";
cin>>days;

cout<<"Enter the daily rate. \n";
cin>>rate;

cout<<"Enter charges for hospital services. \n";
cin>>charges;

cout<<"Enter hospital medication charges. \n";
cin>>medcharges;

cout<<"Total hospital charges.................."<<addcharges(rate, days, charges, medcharges)<<endl;
}
case 2:
{
cout<<"Enter charges for hospital services. \n";
cin>>charges;

cout<<"Enter hospital medication charges. \n";
cin>>medcharges;

cout<<"Total hospital charges.................."<<addcharges(charges, medcharges)<<endl;
}

return 0;
}

double addcharges (double rate, int days, double charges, double medcharges)
{
return (rate * days) + charges + medcharges;
}

double addcharges (double charges, double medcharges)
{
return charges + medcharges;
}

}

```

The errors are...

```error C2661: 'addcharges' : no overloaded function takes 4 parameters
error C2601: 'addcharges' : local function definitions are illegal
error C2601: 'addcharges' : local function definitions are illegal

```

any help is greatly appreciated!

Is This A Good Question/Topic? 0

## Replies To: Overloading Functions

### #2 dorknexus

• or something bad...real bad.

Reputation: 1256
• Posts: 4,618
• Joined: 02-May 04

## Re: Overloading Functions

Posted 26 October 2006 - 09:22 AM

this error

Quote

error C2661: 'addcharges' : no overloaded function takes 4 parameters

is coming up because of this piece of code

```double addcharges (double rate, int days, double charges, double medcharges)
{
return (rate * days) + charges + medcharges;
}

```

at the top of this source file you have two prototypes for the function addcharges. neither of those prototypes takes 4 parameters,
```	double addcharges(int, double);
double addcharges(double);
```

however you define the function to take 4 parameters later in the program. also,

these errors:

Quote

error C2601: 'addcharges' : local function definitions are illegal
error C2601: 'addcharges' : local function definitions are illegal

are because you are defining a function within another function, main no function can contain the definition of another function, therefore you must either define these before or after main, not within.
Was This Post Helpful? 0

### #3 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Overloading Functions

Posted 26 October 2006 - 09:33 AM

These:

```double addcharges(int, double);
double addcharges(double);

and these:

double addcharges (double rate, int days, double charges, double medcharges)
double addcharges (double charges, double medcharges)
```

don't match, you must have the same amount of parameters for each one. The fist one you have an int and a double and the function has a double, an int, a double and another double. Match them up and all should work well. And by the way, you are missing a } around the main function.
Was This Post Helpful? 0

### #4 livefromyadkin

• New D.I.C Head

Reputation: 0
• Posts: 24
• Joined: 22-September 06

## Re: Overloading Functions

Posted 26 October 2006 - 10:05 AM

Ok, I changed
``` 	double addcharges(int, double, double, double);
double addcharges(double, double);
```

and I got no errors. Then trying to compile it I got 2 fatal errors.

```unresolved external symbol "double __cdecl addcharges(int,double,double,double)" (?addcharges@@YANHNNN@Z)

: fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

```

which obviously has to do with the addcharges(int, double, double, double)... I'm stumped!

p.s I fix the brace also...thanks!

This post has been edited by livefromyadkin: 26 October 2006 - 10:07 AM

Was This Post Helpful? 0

### #5 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Overloading Functions

Posted 26 October 2006 - 10:17 AM

They should look like this:

```double addcharges(double, int, double, double);
double addcharges(double, double);

double addcharges (double rate, int days, double charges, double medcharges)
{
return (rate * days) + charges + medcharges;
}

double addcharges (double charges, double medcharges)
{
return charges + medcharges;
}
```

It works on microsoft VC++ express and codeblocks
Was This Post Helpful? 0

### #6 livefromyadkin

• New D.I.C Head

Reputation: 0
• Posts: 24
• Joined: 22-September 06

## Re: Overloading Functions

Posted 26 October 2006 - 10:25 AM

ok now I got it! Thank you SO MUCH for your help!!!
Was This Post Helpful? 0

### #7 BitByte

• D.I.C Head

Reputation: 11
• Posts: 194
• Joined: 09-August 06

## Re: Overloading Functions

Posted 26 October 2006 - 10:34 AM

No problem, glad you got it
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }