1

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;
        }
    }
}
  • " it gives me a ton of errors." Man, if only there were some way for you to communicate what those errors were! Chances are, those errors are there to tell you exactly what went wrong. – Brian Cain Sep 27 '13 at 04:44
  • there is std::max available, you are pulling all namespace from std, it may conflict. – billz Sep 27 '13 at 04:45
  • @BrianCain the errors say term does not evaluate to a function taking 2 arguments seen here http://i.imgur.com/7aBdtSI.png – user2822028 Sep 27 '13 at 04:49
  • Why does every textbook tell people to use the `using namespace std;` directive, just so they can be told otherwise by everyone else. Baffling. – Aesthete Sep 27 '13 at 04:55

3 Answers3

2
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!

Community
  • 1
  • 1
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
0

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.

Mike Weir
  • 3,094
  • 1
  • 30
  • 46
  • I'm not familiar with that method we haven't convered that in class yet. Here is the question by the way. http://i.imgur.com/hGs0ade.png – user2822028 Sep 27 '13 at 04:59
0

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()

nemasu
  • 426
  • 2
  • 10