Search code examples
c++stdbitset

c++ Include <bitset> in linux, and gcc raises error which occurs within <bitset>?


I try to use bitset in my c++ code, the desired behavior is my code man pass compiling and use bitset correctly.

The specific problem is while compiling gcc raise this:

g++ -g -Wall -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine -DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB  -DHOST_i386 -DCHANGED  -c ../threads/synch.cc
In file included from ../machine/machine.h:28:0,
                 from ../threads/thread.h:47,
                 from ../threads/synch.h:21,
                 from ../threads/synch.cc:26:
/usr/include/c++/4.6/bitset: In member function 'void std::bitset<_Nb>::_M_copy_from_ptr(const _CharT*, std::size_t, std::size_t, std::size_t, _CharT, _CharT)':
/usr/include/c++/4.6/bitset:1371:30: error: expected unqualified-id before '(' token
/usr/include/c++/4.6/bitset:1371:30: error: expected unqualified-id before '(' token
/usr/include/c++/4.6/bitset:1371:30: error: expected unqualified-id before '(' token

what does " expected unqualified-id before '(' token" mean, and how do I fix this?

1371 line of file bitset is :

// Definitions of non-inline member functions.
  template<size_t _Nb>
    template<class _CharT, class _Traits>
      void
      bitset<_Nb>::
      _M_copy_from_ptr(const _CharT* __s, size_t __len,
                       size_t __pos, size_t __n, _CharT __zero, _CharT __one)
      {
        reset();
        const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
        for (size_t __i = __nbits; __i > 0; --__i)
          {
            const _CharT __c = __s[__pos + __nbits - __i];
            if (_Traits::eq(__c, __zero))
              ;
            else if (_Traits::eq(__c, __one))
              _Unchecked_set(__i - 1);
            else
              __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
          }
      }

Solution

  • Hard to say from just this snippet, but pretty much the only reason syntax errors happen in system headers is that your code defines a macro that somehow conflicts with the code.

    The most likely candidate here is min. A macro by this name is unfortunately quite wide-spread; e.g. windows.h defines one unless you define NOMINMAX before including it. There are other headers that do the same thing.

    One thing you can do to diagnose this is to put this code snippet after every individual include in your source files:

    #if defined(min)
    #error Previous include defined min
    #endif
    

    Then once you know which header is responsible, you can try to find out how to prevent it from doing that (or #undef the macro if necessary).