我必须写一个binary_print(outstream& outs,unsigned int a)
的函数
但是当我为该函数编写测试文件时,它会显示错误。
#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';}
}
错误:
testfile_rec.cpp:18:60:错误:不匹配&#39;运算符&lt;&lt;&lt;&#39;在 &#39;的std ::运营商LT;&LT; [与_Traits = std :: char_traits](((std :: basic_ostream&amp;)(&amp; std :: cout)), ((const char *)&#34;你的号码的二进制是&#34;))&lt;&lt; binary_print(((std :: ostream&amp;)(&amp; std :: cout)),d)&#39; ostream:108:7:注意: 候选人是:std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; 的std :: basic_ostream&LT; _CharT, _Traits&gt; :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp;(*)(std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp;))[with _CharT = char,_ Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:117:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT, Traits&gt; :: _ios_type&amp;(*)(std :: basic_ostream&lt; _CharT, Traits&gt; :: _ios_type&amp;))[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ios_type = std :: basic_ios] ostream:127:7:注意:
std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: ios_base&amp; (*)(std :: ios_base&amp;))[与_CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:165:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long int)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:169:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long unsigned int)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:173:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(bool)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream.tcc:91:5:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(short int)[with _CharT = char,_ Traits = std :: char_traits] ostream:180:7:注意:std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(short unsigned int) [使用_CharT = char,_Traits = std :: char_traits, std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream.tcc:105:5:注意:
std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;的std :: basic_ostream&LT; _CharT, _Traits&gt; :: operator&lt;&lt;(int)[with _CharT = char,_Traits = std :: char_traits] ostream:191:7:注意:
std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(unsigned int)[with _CharT = char,_ Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:200:7:注意:
std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long long int)[with _CharT = char,_ Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:204:7:注意:
std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long long unsigned int)[与_CharT = char,_Traits = std :: char_traits, std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:209:7:注意:
std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(double)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:213:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(float)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:221:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long double)[with _CharT = char,_Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream:225:7:注意:std :: basic_ostream&lt; _CharT, 性状&GT; :: _ostream_type&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(const void *)[with _CharT = char,_ Traits = std :: char_traits,std :: basic_ostream&lt; _CharT, Traits&gt; :: _ostream_type = std :: basic_ostream] ostream.tcc:119:5:注意:std :: basic_ostream&lt; _CharT, _Traits&GT;&安培; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT, Traits&gt; :: _streambuf_type *)[with _CharT = char,_ Traits = std :: char_traits,的std :: basic_ostream&LT; _CharT, Traits&gt; :: _streambuf_type = std :: basic_streambuf]
答案 0 :(得分:1)
cout
是class ostream
的对象,表示标准输出流。
您的函数无法获取outstream
类型的参数。功能声明:
binary_p(outstream& outs,unsigned int a)
应该是:
binary_p(std::ostream& outs,unsigned int a)
^^^^^^^^
此外,您需要告诉您的程序定义cout
和cin
的命名空间。
做:
using std::cout;
using std::cin;
在您的c ++文件中。 我不确定这是唯一的错误,因为您没有在问题中发布实际错误。
答案 1 :(得分:1)
您可能不应该将其与您的ostream <<
电话一起使用。
cout << "Binary of your number is ";
binary_print(cout,d);
cout << '\n';
编辑:实际上您现在发布的错误表明编译器无法确定调用binary_print()的结果所使用的operator<<
。该错误基本上表示“错误:operator<<
中的std::cout<<"Binary of your number is "<<binary_print(cout,d);
'不匹配,然后列出它尝试匹配的所有替代品。
答案 2 :(得分:1)
如果要打印给定整数的二进制表示,那么简单的方法是使用std::bitset
:
#include<bitset> //include this so as to use std::bitset
unsigned int input;
std::cin >> input;
std::cout << std::bitset<32>(input) << std::endl;
这将打印存储在input
中的值的32位二进制表示。写这个更好:
#include <bitset> //for std::bitset
#include <climits> //for CHAR_BIT
std::cout << std::bitset<CHAR_BIT * sizeof(input)>(input) << std::endl;
现在,您可以将此功能包含在以下函数中:
template<typename T>
void binary_print(std::ostream & out, const T & input)
{
out << std::bitset<CHAR_BIT * sizeof(T)>(input) << std::endl;
}
测试代码:
int main() {
int input;
std::cin >> input;
binary_print(std::cout, input); //print int-representation
binary_print(std::cout, (short)input); //print short-representation
return 0;
}
输出:
00000000000000000010001000010100
0010001000010100