By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,040 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,040 IT Pros & Developers. It's quick & easy.

one bitset simple question

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
Hunter Hou wrote:
std::bitset<6> b1("101010");


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

P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.