#include<iostream>
using namespace std;
class Nibble
{
private:
union
{
int number : 4;
};
public:
Nibble(int=0);
friend ostream& operator<<(ostream&,const Nibble&);
friend istream& operator>>(istream&,Nibble&);
};
inline Nibble::Nibble(int n) : number(n) { cout << "constructor.." << endl;}
ostream& operator<<(ostream& nout,const Nibble& n)
{
return nout << n.number;
}
istream& operator>>(istream& nin,Nibble& n)
{
/* error here
// error : In function `std::istream& operator>>(std::istream&, Nibble&)':
//cannot bind bitfield `n->Nibble::<anonymous>.Nibble::<anonymous union>::number' to `int&'
*/
return nin >> n.number;
}
int main()
{
Nibble n1;
cin >> n1;
cout << "Value of Nibble is:" << n1 << endl;
getchar();
return 0;
}
++C LOVER
0
Newbie Poster
Recommended Answers
Jump to PostPost the errors and a description of your problem/question.
edit: Sorry, didn't see the comments in your code. Still good to post a description of the issue though.
I see a couple of strange things, but the one related to your union:
union { int …
Jump to PostWhat are you trying to do with the colon? Also, I don't believe there's a point to a union with only one element.
I'd like to know this myself. I typically don't see the colon used in unions/structs. I guess it's something I need to study.
I think the error …
Jump to PostWell, I don't agree with this. The streams are declared friends of the class Nibble so they should have the right to access private data. Here's the modified code, where the union yields a variable that simply holds an int--
Oh, right. At a glance I thought they were method …
Jump to Post>>Now you can use the usual union membership operator to assign values to individual fields:
NO YOU CAN'T. The union is used to assign the same memory location to several different POD (Plain Old Data) type objects. All the objects in the union below occupy the same space, and …
Jump to PostI have posted the answer yesterday but DaniWeb site discard it (why?).
You can't redefine >> operator for bit fields because of no such animal as a reference to bit field in C++ language.
Use intermediate int variable then assign it to the bit field - but it's …
All 19 Replies
CoolGamer48
65
Posting Pro in Training
Alex Edwards
321
Posting Shark
CoolGamer48
65
Posting Pro in Training
++C LOVER
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ArkM
1,090
Postaholic
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ArkM
1,090
Postaholic
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ArkM
1,090
Postaholic
++C LOVER
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Alex Edwards
321
Posting Shark
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Alex Edwards
321
Posting Shark
ArkM
1,090
Postaholic
Duoas
1,025
Postaholic
Featured Poster
++C LOVER
0
Newbie Poster
Alex Edwards
321
Posting Shark
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.