Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,391 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,127 people online right now. Registration is fast and FREE... Join Now!




Overloading Functions

 
Reply to this topicStart new topic

Overloading Functions, help please

livefromyadkin
26 Oct, 2006 - 07:40 AM
Post #1

New D.I.C Head
*

Joined: 22 Sep, 2006
Posts: 24


My Contributions
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...

CODE
#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...

CODE

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!
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Overloading Functions
26 Oct, 2006 - 08:22 AM
Post #2

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
this error

QUOTE

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


is coming up because of this piece of code

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,
CODE
    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.
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Overloading Functions
26 Oct, 2006 - 08:33 AM
Post #3

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
These:

CODE
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.
User is offlineProfile CardPM
+Quote Post

livefromyadkin
RE: Overloading Functions
26 Oct, 2006 - 09:05 AM
Post #4

New D.I.C Head
*

Joined: 22 Sep, 2006
Posts: 24


My Contributions
Ok, I changed
CODE
    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.

CODE
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 Oct, 2006 - 09:07 AM
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Overloading Functions
26 Oct, 2006 - 09:17 AM
Post #5

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
They should look like this:

CODE
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
User is offlineProfile CardPM
+Quote Post

livefromyadkin
RE: Overloading Functions
26 Oct, 2006 - 09:25 AM
Post #6

New D.I.C Head
*

Joined: 22 Sep, 2006
Posts: 24


My Contributions
ok now I got it! Thank you SO MUCH for your help!!!
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Overloading Functions
26 Oct, 2006 - 09:34 AM
Post #7

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
No problem, glad you got it biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month