1
votes

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.
$ 
3
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

3 Answers

2
votes

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;

1
votes

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

0
votes

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.