Netbeans: 'Max' Was Not Declared in This Scope

Sorry if my question is very rudimentary.

I have a simple class defined as:

 #ifndef PAYOFF1_H 
 #define PAYOFF1_H 


class PayOff 
 { 
 public: 
 enum OptionType {call,put}; 
 PayOff(double Strike_,OptionType TheOptionType_); 
 double operator()(double Spot) const; 

 private: 
 double Strike; 
 OptionType TheOptionType; 
 }; 

and the source file is:

#include "PayOff1.h" 
 #include "minmax.h" 

 PayOff::PayOff(double Strike_, OptionType TheOptionType_) 
 : 
 Strike(Strike_),TheOptionType(TheOptionType_) 
 { 

 } 

 double PayOff::operator ()(double spot) const 
 { 
 switch(TheOptionType) 
 { 
 case call: 
 return max((spot - Strike) , 0); 

 case put: 
 return max((Strike-spot) , 0); 

 default: 
 throw("unknown option found."); 

 } 
 } 

I get the error 'max' was not declared in this scope.

Thank you for your help in advance.

Regards,

2 Answers

I think you should specify the namespace you are using, like this:

std::max

or

using namespace std;

So, in the first case that part of code should look like this:

...
case call: 
return std::max((spot - Strike) , 0); 

case put: 
return std::max((Strike-spot) , 0); 
...

In the second case:

#include "PayOff1.h" 
#include "minmax.h" 
using namespace std;
...

Don't forget about namespace, it is important.

2

Problem solved! Replacing "minmax.h" with "algorithm" and using std::max. Thanks,

Your Answer

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

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest