numeric_limits<streamsize>

This is a discussion on numeric_limits<streamsize> within the C++ Programming forums, part of the General Programming Boards category; This code compiles and works fine with Dev C++, but MSVC.NET is giving me errors: Code: #include <iostream> ... using ...

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    numeric_limits<streamsize>

    This code compiles and works fine with Dev C++, but MSVC.NET is giving me errors:
    Code:
    #include <iostream>
    ...
    using namespace std;
    ...
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    MSVC reports:
    Code:
    c:\SwitchConf\selectSwitch.cpp(30): error C2059: syntax error : '::'
    c:\SwitchConf\selectSwitch.cpp(30): error C2065: 'numeric_limits' : undeclared identifier
    c:\SwitchConf\selectSwitch.cpp(30): error C2275: 'std::streamsize' : illegal use of this type as an expression
            c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\iosfwd(22) : see declaration of 'std::streamsize'
    c:\SwitchConf\selectSwitch.cpp(30): error C2589: '(' : illegal token on right side of '::'
    c:\SwitchConf\selectSwitch.cpp(30): warning C4003: not enough actual parameters for macro 'max'
    Googling for numeric_limits<streamsize> turned up nothing about MSVC. What's the deal?
    Away.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    #include<limits>
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You need to include <limits>. That's where the numeric_limits template is defined.
    My best code is written with the delete key.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    while we are on this subject, I tried googling for other stuff we could get limits of besides streamsize. the limits library seems kinda confusing (even MSDN didn't over this library well at all) If anyone has a link to a good site or a few examples that would be cool.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Quote Originally Posted by Prelude
    You need to include <limits>. That's where the numeric_limits template is defined.
    That solved most of the errors, but it's still giving me
    Code:
    c:\SwitchConf\selectSwitch.cpp(31): warning C4003: not enough actual parameters for macro 'max'
    c:\SwitchConf\selectSwitch.cpp(31): error C2059: syntax error : ')'
    c:\SwitchConf\selectSwitch.cpp(31): error C2143: syntax error : missing ')' before '::'
    c:\SwitchConf\selectSwitch.cpp(31): error C2589: '(' : illegal token on right side of '::'
    which is odd, since Dev C++ didn't complain (or perhaps that's the part that's odd).
    Away.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Post a complete program that gives you the error. Also, what's the precise version of Visual C++ that you're using? .NET 2003, I would assume?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by confuted
    That solved most of the errors, but it's still giving me
    Code:
    c:\SwitchConf\selectSwitch.cpp(31): warning C4003: not enough actual parameters for macro 'max'
    c:\SwitchConf\selectSwitch.cpp(31): error C2059: syntax error : ')'
    c:\SwitchConf\selectSwitch.cpp(31): error C2143: syntax error : missing ')' before '::'
    c:\SwitchConf\selectSwitch.cpp(31): error C2589: '(' : illegal token on right side of '::'
    which is odd, since Dev C++ didn't complain (or perhaps that's the part that's odd).
    Dev C++ is correct in not complaining.

    Presumably you're #include'ing a MSVC header which #define's a macro named max.

    If that macro is in a standard header, then you need to report a bug to Microsoft: the macro will prevent actual functions (whether class members or not) named max() from working correctly, so will prevent the standard library from working correctly.

    If it is in a non-standard header that comes with MSVC (i.e. it is compiler specific) you need to work out how to avoid using that header along with standard functions (you can often - but not always - achieve that by breaking your code into multiple source files which use different headers).

    A workaround (regardless of whether the problem is in a standard header or a Microsoft specific one) is this trick;
    Code:
    #include <the_offending_header>
    #ifdef max
    #undef max
    #endif
    
    // #include the other headers you need
    Depending on what macros are #define'd in the offending header, you may need to #undef some other ones as well.

    The #ifdef ... #endif wrapper for the #undef is optional, but makes the code a little safer (eg if Microsoft fixes the offending header in a future release of the compiler/library).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The min and max macros are defined within a subheader of <windows.h>, and it will most assuredly never get fixed. (Too much code using it.) You can #define NOMINMAX before including anything in order to prevent the header from defining these macros. It would be best to define it in the project options, so as to avoid source clutter.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21