Thread: library question max(val, 255)

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    library question max(val, 255)

    I've got this code 'snippet' that is giving me problems. Isn't max(val, 255) part of the <iostream> library? I get an error when I use it like this:

    Code:
    val=max(min(val,0),255);
    Says that 'max' : undeclared identifier ... and
    'min' : undeclared identifier

    I've been looking at libraries to see if there is another I am supposed to include, but I haven't found anything helpful.

    tms

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Isn't max(val, 255) part of the <iostream> library?
    No, std::max and std::min are from <algorithm>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    OK, so I just added #include <algorithm> and I am still getting this error message....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Show the smallest and simplest program that demonstrates the error.

    EDIT:
    By the way, I think std::max(std::min(val, 0), 255) will always return 255.
    Last edited by laserlight; 09-09-2007 at 10:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Not a hundred percent sure here, but shouldn't you use std::max instead of just max? Or have you included the std namespace?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is an error with VC++ 6.0 and maybe some other compilers and versions where max and min are #define'd symbols. That means they override any max and min function from <algorithm>. There is something you can define in your program so that it will work. It might be
    Code:
    #define NO_MIN_MAX
    above your includes, but I'm not sure on the details. If that doesn't work then your compiler and version would be helpful to know.

    Also, using std::max and std::min, even if you have a using namespace std in your code, can prevent name conflicts with other usages of max and min (that aren't #define symbols).

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by laserlight View Post
    EDIT:
    By the way, I think std::max(std::min(val, 0), 255) will always return 255.
    Will it do so if val is of type signed char?
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Will it do so if val is of type signed char?
    It may not even compile due to mismatch of template parameter and arguments. My assumption is that val is of type int, but it may not be, which is why tms43 should have posted an example program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  3. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  4. n00b question regarding the Map Class in the STL library
    By Axegrinder#9 in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 09:40 PM
  5. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM