Function Overloading does not work?

A

Anil Gupte

I get two errors
void DisplayProjectedValue(double amount, int years)
{
Random __gc * r = new Random();
int randomRate = r->Next(0, 20);
DisplayProjectedValue(amount, years, randomRate);
}

void DisplayProjectedValue(double amount, int years, double rate)
{
double rateFraction = 1 + (rate/100);
double finalAmount = amount * Math::pow(rateFraction, years);
finalAmount = Math::Round(finalAmount, 2);
Console::WriteLine(S"--------------------------------------------");
Console::WriteLine(S"Investment Amount: ");
Console::Write(amount);
Console::WriteLine(S"Growth Rate [%]: ");
Console::Write(rate);
Console::WriteLine(S"Period [years]: ");
Console::Write(years);
Console::WriteLine (S"Projected Final value of Investment: ");
Console::Write(finalAmount);
Console::WriteLine(S"--------------------------------------------");
}

I tried changing the last line of the first function to:
DisplayProjectedValue(amount, years, randomRate->ToDouble(0));
but that also gave me errors.

This is from a book Visual C++ .Net by Templeman and Olsen (Microsoft
Press).
 
A

Anil Gupte

I should add the error I am getting is: error C2660: 'DisplayProjectedValue'
: function does not take 3 arguments

TIA,
 
B

Bo Persson

Anil Gupte wrote:
:: I get two errors
:: void DisplayProjectedValue(double amount, int years)
:: {
:: Random __gc * r = new Random();
:: int randomRate = r->Next(0, 20);
:: DisplayProjectedValue(amount, years, randomRate);
:: }
::

At this point, the three parameter overload is not known (yet). You
could either change the order, or add a declaration fo the second
function so it is seen before the first one.


Bo Persson


:: void DisplayProjectedValue(double amount, int years, double rate)
:: {
:: double rateFraction = 1 + (rate/100);
:: double finalAmount = amount * Math::pow(rateFraction, years);
:: finalAmount = Math::Round(finalAmount, 2);
::
Console::WriteLine(S"--------------------------------------------");
:: Console::WriteLine(S"Investment Amount: ");
:: Console::Write(amount);
:: Console::WriteLine(S"Growth Rate [%]: ");
:: Console::Write(rate);
:: Console::WriteLine(S"Period [years]: ");
:: Console::Write(years);
:: Console::WriteLine (S"Projected Final value of Investment: ");
:: Console::Write(finalAmount);
::
Console::WriteLine(S"--------------------------------------------");
:: }
::
 
D

David Wilkinson

Anil said:
I get two errors
void DisplayProjectedValue(double amount, int years)
{
Random __gc * r = new Random();
int randomRate = r->Next(0, 20);
DisplayProjectedValue(amount, years, randomRate);
}

void DisplayProjectedValue(double amount, int years, double rate)
{
double rateFraction = 1 + (rate/100);
double finalAmount = amount * Math::pow(rateFraction, years);
finalAmount = Math::Round(finalAmount, 2);
Console::WriteLine(S"--------------------------------------------");
Console::WriteLine(S"Investment Amount: ");
Console::Write(amount);
Console::WriteLine(S"Growth Rate [%]: ");
Console::Write(rate);
Console::WriteLine(S"Period [years]: ");
Console::Write(years);
Console::WriteLine (S"Projected Final value of Investment: ");
Console::Write(finalAmount);
Console::WriteLine(S"--------------------------------------------");
}

Anil:

In C++, a definition or declaraion of a function must appear before it
can be used.
 
A

Anil Gupte

That worked, but that is vey weird - seems like the good old linear Fortran
Programming days! Why should the position in a file have anything to do
with a function being recognized?

Also, why does this line not generate at least a warning if not an error?

DisplayProjectedValue(amount, years, randomRate);

when randomRate is an int and the function is expecting a double?

Thanx,

--
Anil Gupte
www.keeninc.net
www.icinema.com

David Wilkinson said:
Anil said:
I get two errors
void DisplayProjectedValue(double amount, int years)
{
Random __gc * r = new Random();
int randomRate = r->Next(0, 20);
DisplayProjectedValue(amount, years, randomRate);
}

void DisplayProjectedValue(double amount, int years, double rate)
{
double rateFraction = 1 + (rate/100);
double finalAmount = amount * Math::pow(rateFraction, years);
finalAmount = Math::Round(finalAmount, 2);
Console::WriteLine(S"--------------------------------------------");
Console::WriteLine(S"Investment Amount: ");
Console::Write(amount);
Console::WriteLine(S"Growth Rate [%]: ");
Console::Write(rate);
Console::WriteLine(S"Period [years]: ");
Console::Write(years);
Console::WriteLine (S"Projected Final value of Investment: ");
Console::Write(finalAmount);
Console::WriteLine(S"--------------------------------------------");
}

Anil:

In C++, a definition or declaraion of a function must appear before it can
be used.
 
C

Carl Daniel [VC++ MVP]

Anil said:
That worked, but that is vey weird - seems like the good old linear
Fortran Programming days! Why should the position in a file have
anything to do with a function being recognized?

Because C, and subsequently C++ were defined to make it possible for the
compiler to pass over the source code 1 time.
Also, why does this line not generate at least a warning if not an
error?

It does generate an error - you already posted it.
DisplayProjectedValue(amount, years, randomRate);

when randomRate is an int and the function is expecting a double?

You don't get an error for passing an int where a double is expected because
the conversion from int to double is implicit and lossless.

-cd
 
C

Carl Daniel [VC++ MVP]

Anil said:
I get two errors
void DisplayProjectedValue(double amount, int years)
{
Random __gc * r = new Random();

Side note - this is based on the old "Managed Extensions for C++" introduced
with VC7, which are now deprecated. Get a newer book that covers "C++/CLI"
introduced with VC8, which is the going-forward solution for managed
programming in C++.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top