user2822028
user2822028

Reputation: 11

overloaded function with argument evaluation error

So I'm learning C++ and I have to create an overloaded function and from that take the largest number from a set of numbers. This works with 2 numbers but when I comment out the call to the function for 2 numbers and try the one with 3 numbers it gives me a ton of errors. I need a fresh pair of eyes to look at my code and see what I am doing wrong.

#include <iostream>
using namespace std;

// function prototypes
double max(double numberOne, double numberTwo);
double max(double numberOne, double numberTwo, double numberThree);

int main()
{
    int numberOne,
        numberTwo,
        numberThree;

    // user input
    cout << "Input number 1: " << endl;
    cin >> numberOne;
    cout << "Input number 2: " << endl;
    cin >> numberTwo;
    cout << "Input number 3: " << endl;
    cin >> numberThree;

    cout << "The largest of " << numberOne << " and " << numberTwo << ": " << max(numberOne, numberTwo) << endl;
    cout << "The largest of " << numberOne << ", " << numberTwo << ", and " << numberThree << ": " << max(numberOne, numberTwo, numberThree) << endl;

    return 0;
}

// function declarations
double max(double numberOne, double numberTwo) {
    // if number one is greater than number two return that
    // otherwise return numberTwo as the greater value
    if (numberOne > numberTwo) {
        return numberOne;
    } else {
        return numberTwo;
    }
}

double max(double numberOne, double numberTwo, double numberThree) {
    // compare 1 and 2 to see which is greater
    if (numberOne > numberTwo) {
        if (numberOne > numberThree) {
            // 2 is greater return it
            return numberOne;
        } else {
            // else 3 is greater return that
            return numberThree;
        }
    } else {
        if (numberTwo > numberThree) {
            // 2 is greater return it
            return numberTwo;
        } else {
            // else 3 is greater return that
            return numberThree;
        }
    }
}

Upvotes: 1

Views: 229

Answers (3)

Brian Cain
Brian Cain

Reputation: 14619

using namespace std;

// function prototypes
double max( ... )

You are encountering a conflict with the std::max by defining a function with that name in this context.

Either put your max into its own namespace, don't import std, or both. As a compromise, you can using individual names that you require from std instead of all of them. Like so:

using std::vector;
using std::map;
using std::string;

As an aside -- I'm floored -- it's as if I summoned this question myself very recently!

Upvotes: 2

nemasu
nemasu

Reputation: 426

The problem is that max is ambiguous.

You can either rename your functions ( to something like my_max ),

or use the scope resolution operator to call your ( global ) version eg. ::max()

Upvotes: 0

Mike Weir
Mike Weir

Reputation: 3189

Use ::max() instead of max() to access your version of it, so it does not conflict with std::max(). Like so:

cout << "The largest of " << numberOne << " and " << numberTwo << ": " << ::max(numberOne, numberTwo) << endl;
cout << "The largest of " << numberOne << ", " << numberTwo << ", and " << numberThree << ": " << ::max(numberOne, numberTwo, numberThree) << endl;

Though, a more elegant solution would be to actually leverage std::max() and/or use a different name for your functions.

Upvotes: 0

Related Questions