spot the bug

U

urocrane

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;
}
 
A

Alf P. Steinbach

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.
 
M

MiniDisc_2k2

urocrane said:
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.
 
J

John Carson

urocrane said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top