-2

Im trying to learn stl:bitmap, but I'm getting the following error: Headers added - bitset - string

Ive searched other SO posts for this error, but they are not related to bitset.

My Code

int main(){
    bitset<size> bs;
    bitset<10> bs2(45);
    bitset<13> bs3("1100101");

    cout << "bs: " << bs << endl;
    cout << "bs1: " << bs2 << endl;
    cout << "bs2: " << bs3 << endl;
    cout << endl;

    cout << "bs has " << bs.count() << " set bits" << endl;

    cout << bs.size() << endl;
    cout << bs2.size() << endl;
    cout << bs3.size() << endl;
}

My Error: Error in the last 3 cout statements.

$ g++ test.cpp 
test.cpp:28:16: error: expected unqualified-id
    cout << bs.size() << endl;
               ^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
             ^
test.cpp:29:17: error: expected unqualified-id
    cout << bs2.size() << endl;
                ^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
             ^
test.cpp:30:17: error: expected unqualified-id
    cout << bs3.size() << endl;
                ^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
             ^
3 errors generated.
$ 
Amit Kumar
  • 804
  • 2
  • 11
  • 16
  • 5
    Please post a complete program next time, I guess you actually had some lines before `int main()` but did not bother to post them – M.M Sep 07 '18 at 08:09

3 Answers3

2

Remove the #define size 16 from your program. I guess you've written this line at the top of your program.

size macro that you'defined conflicts with size() member function. Use const variable instead of macros. You should use const int size=16;

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • @AmitKumar: because `bitset bs;` is wrong, You need to pass constant size value not variable – Destructor Sep 07 '18 at 08:14
  • I replaced `#define size 16` with `const int size=16;` and it works now! Thanks – Amit Kumar Sep 07 '18 at 08:15
  • @AmitKumar: It'll be better if you accept this answer as right answer. I feel glad to help you. – Destructor Sep 07 '18 at 08:16
  • sir I think [this](https://stackoverflow.com/a/52218184/5767939) answer here solve the problem in general. – Amit Kumar Sep 07 '18 at 08:23
  • I've said the similar thing in few words. I've clearly said that size macro conflicts with size() member function. Did I say anything different than the answer you are talking about ? It is your choice whose answer you want to accept. – Destructor Sep 07 '18 at 08:25
  • 1
    This is the better answer - while there are some techniques to reduce the problems with macro's, none of them fully eliminate the problems. Macro's remain untyped, ignore namespaces, and are not evaluated per common C++ rules. They are literally a distinct language in a language. – MSalters Sep 07 '18 at 08:43
1

You seem to have a macro defined in test.cpp line 6 which is string replacing your attempt to call the function size.

Your line is actually saying:

cout << bs.16() << endl;
cout << bs2.16() << endl;
cout << bs3.16() << endl;

If you want to use macro's it's good practice to make them as descriptive as possible and use ALL_UPPER_CASE to avoid these type of issues.

e.g. #define BITSET_DEFAULT_SIZE 16

The error descriptions given to you by the compiler are very descriptive and let you know that a macro is the reason for this problem:

test.cpp:28:16: error: expected unqualified-id
    cout << bs.size() << endl; <- this is telling you the starting position of the error
               ^
test.cpp:6:14: note: expanded from macro 'size'
    #define size 16 <- this is telling you a macro is involved, and giving its value

Also, it's not good practice to use using namespace std in your programs due to std containing so many generic named functions. For example, if you create a function called size, you've suddenly overwritten std::size.

Here is a good post pointing out why this is a bad idea

Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • 2
    The error is due to your macro being too generic in name, a macro is the same as a string replace in the code before the code is compiled. If you call your macro the same name as a function you will be unable to call that function in code where that macro is defined. This is why macros are dangerous unless you name them specifically and why its good practice to use ALL_UPPER_CASE when defining a macro, because funciton names rarely follow that pattern. – Serdalis Sep 07 '18 at 08:17
0

Use

#undef size 

immediately after the line

bitset<size> bs;

This will hide your macro and the rest of the code should now compile.

NOTE: This is not a permanent fix. But in case the macro is in a header file which is included in many files, this will give a temporary fix. But using const in C++ is recommended over a macro.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • From what I see this error is not due to using macro, but using a generic name for macro as answered by [Serdalis](https://stackoverflow.com/users/958051/serdalis), [here](https://stackoverflow.com/a/52218184/5767939). – Amit Kumar Sep 07 '18 at 08:20
  • I faced similar issue before and this worked. But it is not a permanent fix. But in case the macro is in a header file which is included in many files, this will give a temporary fix. But using const in C++ is recommended over a macro. Will update the answer to reflect this. – P.W Sep 07 '18 at 08:26