使用 VS2010 的 C++ 中的位集 [英] bitsets in C++ using VS2010
问题描述
我不知道我在这里做错了什么,希望这里有人能启发我.
我有一个 Flags
类,这是一个极其简化的版本,但我声明了一个位集
类标志{私人的://8位列表std::bitset<8>_P;民众:标志();}
在我的构造函数中,我将其初始化为
Flags::Flags():_P(32ul){}
但它不会编译并给我错误
<块引用>错误 C2668:'std::bitset<_Bits>::bitset':对重载函数
这是在 VS2010 SP1 64 位编译,但作为 32 位程序
编辑
接受的答案是上述但作为旁注,任何人都可以解释为什么在使用默认构造函数(应该将它们全部初始化为零)时,它们并未全部设置为零?
_p.to_ulong()
返回 1390560944和 _p 看起来像
[8](0,0,0,0,1,1,0,1)
另请注意,以 _
和大写字母开头的标识符是为实现保留的,在您的程序中使用是非法的.
根据变通方法页面(如果我没看错的话),变通方法是将您的值转换为 unsigned long long
而不是 unsigned long
.>
I can't figure out if I'm doing anything wrong here, hopefully someone here can enlighten me.
I have a class Flags
, this is an extremely simplified version but I declare a bitset
class Flags
{
private:
//List of 8 bits
std::bitset<8> _P;
public:
Flags();
}
On my constructor I initialise it as
Flags::Flags()
: _P(32ul)
{}
But it won't compile and gives me the error
error C2668: 'std::bitset<_Bits>::bitset' : ambiguous call to overloaded function
This is compiled in VS2010 SP1 64 bit but as a 32bit program
EDIT
Accepted answer is for the above but as a side note could anyone explain why when using the default constructor (which should initialise them all to zero's) they aren't all set to zero's?
_p.to_ulong()
returns 1390560944 and _p looks like
[8](0,0,0,0,1,1,0,1)
It's a bug in VC according to http://connect.microsoft.com/VisualStudio/feedback/details/532897/problems-constructing-a-bitset-from-an-unsigned-long-in-the-vc-rc
Also note that identifiers starting with _
and a capital letter are reserved for the implementation and illegal to use in your program.
EDIT: According to the workaround page (if I read it right), the workaround is to cast your value to unsigned long long
instead of unsigned long
.
这篇关于使用 VS2010 的 C++ 中的位集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!