473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

one bitset simple question

Hello,

I have this very simple program, but it can't be compiled. What's wrong
here?

thanks,
hunter
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_Nb>::bitset(long
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit constructor.

P.S. > If I use default constructor, everything is fine.
Jul 22 '05 #1
6 2297
"Hunter Hou" <hy***@lucent.com> wrote in message
news:cb********@netnews.proxy.lucent.com...
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_Nb>::bitset(long
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit

constructor.

No it doesn't. It has a template constructor that takes a string.
Try:

std::bitset<6> b1(string("101010"));

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #2
Hunter Hou wrote:
std::bitset<6> b1("101010");


Try:
std::bitset<6> b1(string("101010"));
Jul 22 '05 #3
Hunter Hou wrote:
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6>*b1("101010");

std::cout*<<*b1*<<*std::endl;

return*0;

}


Try

#include <iostream>
#include <bitset>
#include <string>

int main()
{
std::bitset<6> b1( std::string( "101010" ) );

std::cout << b1 << std::endl;

return 0;

}
Best

Kai-Uwe Bux
Jul 22 '05 #4

"Hunter Hou" <hy***@lucent.com> wrote in message
news:cb********@netnews.proxy.lucent.com...
Hello,

I have this very simple program, but it can't be compiled. What's wrong
here?

thanks,
hunter
#include <iostream>
#include <bitset>

int main()
{
std::bitset<6> b1("101010");

std::cout << b1 << std::endl;

return 0;

}
The error is:
bitset.cpp: In function `int main()':
bitset.cpp:8: invalid conversion from `const char*' to `long unsigned int'
bitset.cpp:8: initializing argument 1 of `std::bitset<_Nb>::bitset(long
unsigned int) [with unsigned int _Nb = 6]'
Compiler says can't convert, but bitset<> has such an explicit constructor.
P.S. > If I use default constructor, everything is fine.


Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying
to initialize the bitset container with an unsigned long integer. You could
declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L = (size_t)(-1))

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}

Also:

#include <iostream>
#include <bitset>

int main()
{
std::bitset<8> b1(0x2A); // 00101010

int sz = b1.size();
for (int i = 0; i < sz; i++) // from b1[7] to b1[0]
{
std::cout << b1[sz - i - 1];
}

std::cout << std::endl;

return 0;
}

Jul 22 '05 #5

Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying
to initialize the bitset container with an unsigned long integer. You could
declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L = (size_t)(-1))
Exactly.

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}


since string can be defined by const char*, why I have to use a string?
because it's explicit?
Jul 22 '05 #6

"Hunter Hou" <hy***@lucent.com> wrote in message
news:cb********@netnews.proxy.lucent.com...

Your compiler is confused, clarify your intentions by first locating the
bitset constructor, if any, that takes a string or char* literal. Its trying to initialize the bitset container with an unsigned long integer. You could declare a std::bitset<8> and initialize it with 0x2A or 42 for a binary
sequence of 00101010.

But fortunately, you shouldn't need to, bitset takes a reference to a
std::string as an arguement to one of its constructors:
in <bitset>.... you will find something like this:

explicit bitset(const string& _S, size_t _P = 0, size_t _L =
(size_t)(-1))
Exactly.

so try declaring a std::string and initializing it with "101010".

#include <iostream>
#include <bitset>

int main()
{
std::string s1("101010");
std::bitset<6> b1(s1);

std::cout << b1 << std::endl;

return 0;
}


since string can be defined by const char*, why I have to use a string?
because it's explicit?


Thats correct, an implicit conversion is not allowed. Its true for a
templated constructor as well since a templated cstor implies the explicit
keyword.

You may find section 9.4 in the following URL usefull:
http://www.icce.rug.nl/documents/cpl...us09.html#l142

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy Skypeck | last post by:
I have a question concerning the use of the operator in bitset asignment. bitset<4> a("1100"); bitset<4> b("0011"); a = a; Is clearly supported by the standard, and works with all the C++...
3
by: Jason | last post by:
hello, I've been looking into the idea of using a bitset to store a collection of fields efficiently in a data structure that uses a large number of elements. My question is what technique is...
2
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is...
10
by: halfsearch2 | last post by:
Hi all, I want to sort a bitset vector ( bit 1 > bit 0 ), while it seems bitset doesn't have < or > operation. I tried to use a loop to compare each bit one by one but it's unbearably slow. Any...
5
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets...
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
7
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.