Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > one bitset simple question

Reply
Thread Tools

one bitset simple question

 
 
Hunter Hou
Guest
Posts: n/a
 
      06-24-2004
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.


 
Reply With Quote
 
 
 
 
Bill Seurer
Guest
Posts: n/a
 
      06-24-2004
Hunter Hou wrote:

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


Try:
std::bitset<6> b1(string("101010"));
 
Reply With Quote
 
 
 
 
P.J. Plauger
Guest
Posts: n/a
 
      06-24-2004
"Hunter Hou" <(E-Mail Removed)> wrote in message
news:cbe9q4$(E-Mail Removed)...

> 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


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-24-2004
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
 
Reply With Quote
 
SaltPeter
Guest
Posts: n/a
 
      06-24-2004

"Hunter Hou" <(E-Mail Removed)> wrote in message
news:cbe9q4$(E-Mail Removed)...
> 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;
}



 
Reply With Quote
 
Hunter Hou
Guest
Posts: n/a
 
      06-25-2004

>
> 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?
 
Reply With Quote
 
SaltPeter
Guest
Posts: n/a
 
      06-25-2004

"Hunter Hou" <(E-Mail Removed)> wrote in message
news:cbg076$(E-Mail Removed)...
>
> >
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitset<32> and bitset<64> efficiency Ninds C++ 14 12-03-2012 11:02 PM
std::bitset, simple question Haro Panosyan C++ 14 12-06-2005 11:16 PM
Is 'java.util.BitSet' available at JDK1.3? Joshua Java 4 05-06-2005 11:54 AM
Performance of java.util.BitSet Timo Nentwig Java 4 12-11-2003 12:43 AM
std::bitset construction with std::string question Dill Hole C++ 2 07-04-2003 11:48 PM



Advertisments