开发者

compilation error with boost dynamic_bitset

I'm trying to use boost::dynamic_bitset, as shown below:

#include <boost/dynamic_bitset.hpp>

class Bitmap
{
public:
  Bitmap(std::size_t size = _size);
  void setBit(int pos);
  void clearBit(int pos);
  bool get(int pos);
  void resize(int size);

private:
  boost::dynamic_bitset<> _bitset(8);
  static const std::size_t _size;
};

I'm getting the following errors, while declaring dynamic_bitset:

test1.cpp:14: error: expected identifier before numeric constant
test1.cpp:14: error: expected ‘,’ or ‘...’ before numeric constant

Boost documentation gives an example here, which compiles absolutely fine. Can somebody point out the problem开发者_如何学JAVA here?

My compiler is g++ version 4.4.5.


The difference is that you are trying to initialize member variable, not "freestanding" one.

Either run with -std=c++0x (see comment at end of post) or do:

// in class definition:
boost::dynamic_bitset<> _bitset;

// in constructor:
Bitmap(/* params */) : _bitset(8) { /* rest of code */ }

Initializing members the way you are trying to do was introduced in C++11. If i remember correctly, g++ 4.4.5 still lacked that feature.


boost::dynamic_bitset<> _bitset(8);
                             //^^^ cause of the problem!

In-class initialization is not allowed in both C++03 and C++98. It is allowed in C++11, however.

So, in pre-C++11, do the initiatialization in the constructor member-initialization-list as:

 Bitmap(std::size_t size = _size): _bitset(8) 
 {                              //^^^^^^^^^^called member-initialization-list
    //...
 }
private:
 boost::dynamic_bitset<> _bitset; //no initialization here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜