0

I am trying to create a program where I overload a function to find the maximum between 2, 3, or 4 parameters. My code seems a bit wonky at the moment, whenever I run it I recieve the dreaded red line under "#include " and the three parameter function call. When I disable it //, the 2 and 4 parameters are fine but my 3 parameter one is no good. Would one of you mind taking a look at it and try to help? Thank you!

#include <cstdlib>
#include <iostream>

using namespace std;


double max (double, double, double, double);


int main(int argc, char** argv) {

double result;

result = max(10.0, 20.0);
cout << "Max(10.0, 20.0) = " << result << endl;
result = max(11.5, 21.2, 5.3);
cout << "Max(11.5, 21.2, 5.3) = " << result << endl;
result = max(1.8, 2.2, 1.7, 2.1);
cout << "Max(1.8, 2.2, 1.7, 2.1) = " << result << endl;
return 0;


}

double max(double a, double b)
{
if (a > b)
    return a;
else 
    return b;
}

double max(double a, double b, double c)
{
if (a > b && a > c)
    return a;

else if (b > a && b > c)
    return b;

else
    return c;
}

double max(double a, double b, double c, double d)
{
if (a > b && a > c && a > d)
    return a;
else if (b > a && b > c && b > d)
    return b;
else if (c > d)
    return c;
else 
    return d;
}
2
  • 2
    max is already defined in the std namespace. Remove using namespace std; from your program. Commented Oct 30, 2017 at 22:28
  • So for 2 variables, std::max is being used, because it is not declared, and for the 4 variable case, the overloaded function is being used. 3 variable function is not declared, so you are getting an error. Commented Oct 30, 2017 at 22:35

3 Answers 3

2

Get rid of using namespace std; you are otherwise causing an ambiguity between your functions and std::max.

In general using namespace std is considered bad practice and causes problems exactly like you are seeing.

Sign up to request clarification or add additional context in comments.

Comments

2

You have not declared the overload with three parameters before you use it.

Add

double max(double, double, double );

before you call it.

You should also declare the overload with two parameters version before you use it but you end up picking a version from the standard library. Getting rid of

using namespace std;

should detect that problem as well.

Comments

2

As-is this will not compile. You are introducing the std namespace into a current scope but there will not be a call to std::max on VS implementation while there will be one on Linux GCC implementation, apparently via the stl_algobase.h header:

/usr/local/include/c++/7.2.0/bits/stl_algobase.h: In instantiation of 'constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare) [with _Tp = double; _Compare = double]':

Provide all three function declarations before the main() function:

double max(double, double);
double max(double, double, double);
double max(double, double, double, double);
int main() {
// same as before
}

Live example on Coliru

That being said don't use the using namespace std; statement.

1 Comment

Yes! I didn't declare my 3 parameter overloaded function and tis is why it is not working! Thank you so much! As for the using namespace std; bit, I'm just following along with what my professor said to use I'm sure we will cover it in great detail at a later date ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.