Hi,
I'm new here so if anything I post is not allowed please correct it for me.
I've been assigned a task of displaying a variable of unsigned int as a binary number in C++. I can do this fine using my own tests.
I have a class Binary, which has just 1 value number, which is of type unsigned.
I then overload the output << operator so that whenever cout << Binaryobject ; is called, the binary value of that number is displayed.
I do this by using a for loop that goes through each bit (all 32 of them). I can display all numbers fine is I use the above call.
So for example:
will print out 10111
However, I have to use a predefined test main method, and herein lies the problem.
The testing method is
This produces the linker errors mentioned above.
If I miss out the entire else statement it compiles fine. It also compiles if I leave the first line of it - output << setw(2) << b.Get() << " = " ;
Anything below that and I have this problem.
The error message I get is
error LNK2019: unresolved external symbol "class std::basic_ostr eam<char,struct std::char_trait s<char> > & __cdecl operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char> > &,class Binary const &)" (??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @ABVBinary@@@Z) referenced in function _main
The main() has just been provided so I'm not sure what requirements it has or why the error comes up.
Could anyone help me?
If you need to see any of my code please ask, but I didn't want to include too much in case it broke rules.
I'm new here so if anything I post is not allowed please correct it for me.
I've been assigned a task of displaying a variable of unsigned int as a binary number in C++. I can do this fine using my own tests.
I have a class Binary, which has just 1 value number, which is of type unsigned.
I then overload the output << operator so that whenever cout << Binaryobject ; is called, the binary value of that number is displayed.
I do this by using a for loop that goes through each bit (all 32 of them). I can display all numbers fine is I use the above call.
So for example:
Code:
Binary b; b = b.Set(23); cout << b << endl;
However, I have to use a predefined test main method, and herein lies the problem.
The testing method is
Code:
int main() { Binary b, b15; unsigned t = 4294967295; ofstream output("test1"); if (output == NULL) cerr << "File cannot be opened" << endl; else { output << setw(2) << b.Get() << " = " << setw(6) << b << endl; for (int i=-28; i<-23;i++) { output << setw(3) << i << " = " << setw(32) << Bin(i) << endl; output << setw(3) << -i << " = " << setw(32) << Bin(-i) << endl; } output << endl; b15.Set(15); output << setw(2) << b15.Get() << " = " << b15 << endl << endl; output << "big small binary number: " << t << " = " << Bin(t) << endl << endl; } cout << "This is fine" << endl; } }
If I miss out the entire else statement it compiles fine. It also compiles if I leave the first line of it - output << setw(2) << b.Get() << " = " ;
Anything below that and I have this problem.
The error message I get is
error LNK2019: unresolved external symbol "class std::basic_ostr eam<char,struct std::char_trait s<char> > & __cdecl operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char> > &,class Binary const &)" (??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @ABVBinary@@@Z) referenced in function _main
The main() has just been provided so I'm not sure what requirements it has or why the error comes up.
Could anyone help me?
If you need to see any of my code please ask, but I didn't want to include too much in case it broke rules.
Comment