spot the bug

Discussion in 'C++' started by urocrane, Jul 12, 2003.

  1. urocrane

    urocrane Guest

    I've been working around a sample program thats "suppose" to work,
    according to Herbert Schildt-but it doesn't... I'm getting a
    redefinition error from struct byte. anyone know whats wrong and how
    to fixit? and i need some indepth tutorials about bitwise operators
    and bitfield.
    struct byte
    {
    unsigned a : 1;
    unsigned b : 1;
    //... all the way to unsigned h : 1;
    };

    union bits
    {
    char ch;
    byte bit;
    } ascii; //this program displays the ascii code in binary for
    characters

    void disp_bits(bits b); /*contains code like if(b.bit.h) cout<<"1 "l;
    else cout<<"0 "; */

    int main()
    {
    do
    {
    cin>>ascii.ch;
    cout<<": ";
    disp_bits(ascii);
    }while(ascii.ch!='q');
    return 0;
    }
     
    urocrane, Jul 12, 2003
    #1
    1. Advertising

  2. On 12 Jul 2003 07:36:11 -0700, (urocrane) wrote:

    >I've been working around a sample program thats "suppose" to work,
    >according to Herbert Schildt-but it doesn't...


    See the FAQ on (I think it was) alt.learn.c or whatever
    group it was, about Schildt's books. Upshot: nearest trash can.
    However, this particular problem doesn't seem to be due to him.


    > I'm getting a
    >redefinition error from struct byte. anyone know whats wrong and how
    >to fixit?


    The code as presented in your posting is technically OK.

    Perhaps your compiler objects to the identifier 'byte'.




    >and i need some indepth tutorials about bitwise operators
    >and bitfield.


    See the C++ FAQ for suggestions about literature.
     
    Alf P. Steinbach, Jul 12, 2003
    #2
    1. Advertising

  3. urocrane

    MiniDisc_2k2 Guest

    "urocrane" <> wrote in message
    news:...
    > I've been working around a sample program thats "suppose" to work,
    > according to Herbert Schildt-but it doesn't... I'm getting a
    > redefinition error from struct byte. anyone know whats wrong and how
    > to fixit? and i need some indepth tutorials about bitwise operators
    > and bitfield.
    > struct byte
    > {
    > unsigned a : 1;
    > unsigned b : 1;
    > //... all the way to unsigned h : 1;
    > };
    >
    > union bits
    > {
    > char ch;
    > byte bit;
    > } ascii; //this program displays the ascii code in binary for
    > characters
    >
    > void disp_bits(bits b); /*contains code like if(b.bit.h) cout<<"1 "l;
    > else cout<<"0 "; */
    >
    > int main()
    > {
    > do
    > {
    > cin>>ascii.ch;
    > cout<<": ";
    > disp_bits(ascii);
    > }while(ascii.ch!='q');
    > return 0;
    > }


    Your compiler may not like "byte." Some compilers have a built-in type byte,
    and thus you would be redefining that keyword. Then again, some nonstandard
    libraries define byte on their own, so that could be a problem. As your
    compiler is saying that byte is being redefined, it seems that it is not a
    keyword. You've probably got some library that's defining byte on its own.
    Ensure only standard libraries:

    This Should be changed to
    #include <iostream.h> #include <iostream>
    using namespace std;
    #include <conio.h> Nothing. do not use it.
    #include <fstream.h> #include <fstream>
    using namespace std;

    Note that you only say "using namespace std;" once.

    --
    MiniDisc_2k2
     
    MiniDisc_2k2, Jul 12, 2003
    #3
  4. urocrane

    John Carson Guest

    "urocrane" <> wrote in message
    news:
    > I've been working around a sample program thats "suppose" to work,
    > according to Herbert Schildt-but it doesn't... I'm getting a
    > redefinition error from struct byte. anyone know whats wrong and how
    > to fixit? and i need some indepth tutorials about bitwise operators
    > and bitfield.
    > struct byte
    > {
    > unsigned a : 1;
    > unsigned b : 1;
    > //... all the way to unsigned h : 1;
    > };
    >
    > union bits
    > {
    > char ch;
    > byte bit;
    > } ascii; //this program displays the ascii code in binary for
    > characters
    >
    > void disp_bits(bits b); /*contains code like if(b.bit.h) cout<<"1 "l;
    > else cout<<"0 "; */
    >
    > int main()
    > {
    > do
    > {
    > cin>>ascii.ch;
    > cout<<": ";
    > disp_bits(ascii);
    > }while(ascii.ch!='q');
    > return 0;
    > }



    Does your compiler error message tell you where byte is first defined? Mine
    does. Clicking on the error message takes me to "RpcNdr.h", which is a file
    included when I include "windows.h". Your problem will be similar, if not
    identical.

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Jul 12, 2003
    #4
    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. A great onestop spot

    , Feb 7, 2006, in forum: Perl
    Replies:
    0
    Views:
    2,071
  2. PerlBoy

    hot spot

    PerlBoy, Feb 22, 2006, in forum: HTML
    Replies:
    1
    Views:
    527
    Disco Octopus
    Feb 22, 2006
  3. JKop
    Replies:
    3
    Views:
    433
    Old Wolf
    Oct 13, 2004
  4. Sathyaish

    Please spot out the cause of this bug

    Sathyaish, Nov 19, 2004, in forum: C Programming
    Replies:
    2
    Views:
    387
    Charlie Gordon
    Nov 24, 2004
  5. Replies:
    5
    Views:
    309
    David Thompson
    May 21, 2007
Loading...

Share This Page