one bitset simple question

Discussion in 'C++' started by Hunter Hou, Jun 24, 2004.

  1. Hunter Hou

    Hunter Hou Guest

    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.
     
    Hunter Hou, Jun 24, 2004
    #1
    1. Advertising

  2. Hunter Hou

    Bill Seurer Guest

    Hunter Hou wrote:

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


    Try:
    std::bitset<6> b1(string("101010"));
     
    Bill Seurer, Jun 24, 2004
    #2
    1. Advertising

  3. Hunter Hou

    P.J. Plauger Guest

    "Hunter Hou" <> wrote in message
    news:cbe9q4$...

    > 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
     
    P.J. Plauger, Jun 24, 2004
    #3
  4. Hunter Hou

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jun 24, 2004
    #4
  5. Hunter Hou

    SaltPeter Guest

    "Hunter Hou" <> wrote in message
    news:cbe9q4$...
    > 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;
    }
     
    SaltPeter, Jun 24, 2004
    #5
  6. Hunter Hou

    Hunter Hou Guest


    >
    > 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?
     
    Hunter Hou, Jun 25, 2004
    #6
  7. Hunter Hou

    SaltPeter Guest

    "Hunter Hou" <> wrote in message
    news:cbg076$...
    >
    > >
    > > 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/cplusplus/cplusplus09.html#l142
     
    SaltPeter, Jun 25, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    Performance of java.util.BitSet

    Timo Nentwig, Dec 9, 2003, in forum: Java
    Replies:
    4
    Views:
    3,964
  2. Joshua
    Replies:
    4
    Views:
    395
    Esmond Pitt
    May 6, 2005
  3. Dill Hole
    Replies:
    2
    Views:
    442
    Mike Wahler
    Jul 5, 2003
  4. Haro Panosyan

    std::bitset, simple question

    Haro Panosyan, Dec 6, 2005, in forum: C++
    Replies:
    14
    Views:
    1,162
    Haro Panosyan
    Dec 6, 2005
  5. Ninds
    Replies:
    14
    Views:
    782
    W Karas
    Dec 3, 2012
Loading...

Share This Page