I had to write a function which binary_print(outstream& outs,unsigned int a)
but when i write a test file for that function it says error.
#include<iostream>
int main()
{
unsigned int d;
std::cout<<"Enter any positive decimal number:";
std::cin>>d;
std::cout<<"Binary of your number is "<<binary_print(cout,d);//<<endl;
std::cout<<'\n';}
}
errors:
testfile_rec.cpp:18:60: error: no match for ‘operator<<‘ in
‘std::operator<< [with _Traits =
std::char_traits](((std::basic_ostream&)(& std::cout)),
((const char*)”Binary of your number is “)) <<
binary_print(((std::ostream&)(& std::cout)), d)’ ostream:108:7: note:
candidates are: std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_ostream_type& (*)(std::basic_ostream<_CharT, Traits>::_ostream_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:117:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_ios_type& (*)(std::basic_ostream<_CharT, Traits>::_ios_type&)) [with _CharT =
char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream, std::basic_ostream<_CharT, Traits>::_ios_type =
std::basic_ios] ostream:127:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&
(*)(std::ios_base&)) [with _CharT = char, _Traits =
std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:165:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:169:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:173:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with
_CharT = char, _Traits = std::char_traits] ostream:180:7: note: std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int)
[with _CharT = char, _Traits = std::char_traits,
std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream.tcc:105:5: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits] ostream:191:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with
_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:200:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with
_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:204:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned
int) [with _CharT = char, _Traits = std::char_traits,
std::basic_ostream<_CharT, Traits>::_ostream_type =
std::basic_ostream] ostream:209:7: note:
std::basic_ostream<_CharT, Traits>::_ostream_type&
std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT =
char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:213:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:221:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream:225:7: note: std::basic_ostream<_CharT,
Traits>::_ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_ostream_type = std::basic_ostream] ostream.tcc:119:5: note: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, Traits>::_streambuf_type*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,
Traits>::_streambuf_type = std::basic_streambuf]
If you want to print the binary representation of the given integer, then easy way is to use
std::bitsetas:This will print 32-bit binary representation of the value stored in
input. It is even better to write this as:Now you can wrap this functionality in a function as:
Test code:
Output:
Online demo : http://ideone.com/OQU6F